Skip to content

Commit

Permalink
Glow: parseRow() method to understand inline comments. #206
Browse files Browse the repository at this point in the history
  • Loading branch information
tipiirai committed Feb 23, 2024
1 parent 493aa13 commit dcdcf1d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 32 deletions.
29 changes: 12 additions & 17 deletions packages/glow/src/glow.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


const MIXED_HTML = ['html', 'jsx', 'php', 'astro', 'nue', 'vue', 'svelte', 'hb']
const LINE_COMMENT = { clojure: ';;', lua: '--', python: '#' }
const PREFIXES = {'+': 'ins', '-': 'del', '>': 'dfn' }
Expand Down Expand Up @@ -30,6 +29,9 @@ const RULES = {

const HTML_TAGS = [

// line comment
{ tag: 'sup', re: /# .+/ },

{ tag: 'label', re: /\[([a-z\-]+)/g, lang: ['md', 'toml'], shift: true },

// string value (keep second on the list)
Expand Down Expand Up @@ -147,9 +149,14 @@ function getMDTags(str) {
]
}


export function parseRow(row, lang) {
const tags = isMD(lang) ? getMDTags(row) : getTags(lang)
const ret = []
const tokens = []

// line comment (language specific)
const re = new RegExp(`${LINE_COMMENT[lang] || '//'} .+`)
tags.unshift({ tag: 'sup', re })


for (const el of tags) {
Expand All @@ -161,17 +168,16 @@ export function parseRow(row, lang) {
match = start; start = n + more
}
const end = start + match.length
ret.push({ start, end, ...el })
tokens.push({ start, end, ...el })
})
}
return ret.sort((a, b) => a.start - b.start)
return tokens.sort((a, b) => a.start - b.start)
}

// exported for testing purposes
export function renderRow(row, lang) {
if (!row) return ''


const els = parseRow(row, lang)
const ret = []
var index = 0
Expand Down Expand Up @@ -252,15 +258,6 @@ export function parseSyntax(str, lang) {
return lines
}

function findCommentIndex(line, lang) {
if (isMD(lang)) return -1

for (const prefix of [LINE_COMMENT[lang] || '//', '#']) {
const i = line.indexOf(prefix + ' ')
if (i >= 0) return i
}
return -1
}

// code, { language: 'js', numbered: true }
export function glow(str, opts={}) {
Expand All @@ -283,9 +280,7 @@ export function glow(str, opts={}) {
return comment.forEach(el => push(elem('sup', encode(el))))

} else {
const i = findCommentIndex(line, lang)
line = i < 0 ? renderRow(line, lang) :
renderRow(line.slice(0, i), lang) + elem('sup', encode(line.slice(i)))
line = renderRow(line, lang)
}

if (wrap) line = elem(wrap, line)
Expand Down
10 changes: 2 additions & 8 deletions packages/glow/test/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,7 @@ test "parse integers" {
var it = std.mem.tokenizeAny(u8, input, " ,");
while (it.next()) |num| {
const n = try parseInt(u32, num, 10);
try list.append(n);
}
const expected = [_]u32{ 123, 67, 89, 99 };
for (expected, list.items) |exp, actual| {
try std.testing.expectEqual(exp, actual);
try list.append(n); // EOL comment
}
}
`
Expand Down Expand Up @@ -702,7 +696,7 @@ await renderPage([
{ title: 'TypeScript', code: TS, lang: 'ts', },
{ title: 'ZIG', code: ZIG, lang: 'zig', },

] // .filter(el => el.lang == 'html')
] // .filter(el => ['zig','clojure'].includes(el.lang))

)

Expand Down
8 changes: 1 addition & 7 deletions packages/glow/test/glow-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,7 @@
<span> <strong>var</strong> <b>it</b> <i>=</i> std<i>.</i>mem<i>.</i><b>tokenizeAny</b><i>(</i>u8<i>,</i> input<i>,</i> <em>" ,"</em><i>)</i><i>;</i></span>
<span> <strong>while</strong> <i>(</i>it<i>.</i><strong>next</strong><i>(</i><i>)</i><i>)</i> <i>|</i>num<i>|</i> <i>{</i></span>
<span> <strong>const</strong> n <i>=</i> <strong>try</strong> <b>parseInt</b><i>(</i>u32<i>,</i> num<i>,</i> <em>10</em><i>)</i><i>;</i></span>
<span> <strong>try</strong> list<i>.</i><b>append</b><i>(</i>n<i>)</i><i>;</i></span>
<span> <i>}</i></span>
<span></span>
<span> <strong>const</strong> <b>expected</b> <i>=</i> <i>[</i>_<i>]</i>u32<i>{</i> <em>123</em><i>,</i> <em>67</em><i>,</i> <em>89</em><i>,</i> <em>99</em> <i>}</i><i>;</i></span>
<span></span>
<span> <strong>for</strong> <i>(</i>expected<i>,</i> list<i>.</i>items<i>)</i> <i>|</i>exp<i>,</i> actual<i>|</i> <i>{</i></span>
<span> <strong>try</strong> std<i>.</i>testing<i>.</i><b>expectEqual</b><i>(</i>exp<i>,</i> actual<i>)</i><i>;</i></span>
<span> <strong>try</strong> list<i>.</i><b>append</b><i>(</i>n<i>)</i><i>;</i> <sup>// EOL comment</sup></span>
<span> <i>}</i></span>
<span><i>}</i></span></code></pre>
</div>
Expand Down

0 comments on commit dcdcf1d

Please sign in to comment.