Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved test, fix for multiple codeblocks, code quality and comments #1

Merged
merged 4 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
tests/result.pango
.idea
.vscode
.bin
dist
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ src/bin/md2pango: src/md2pango.js
cat src/md2pango.js >> $@

TESTS=all
test: ; ./test.sh $(TESTS)
test: ; ./tests/test.sh $(TESTS)
reuse: ; reuse addheader src/*.js -y 2021 -l MIT -c 'Uwe Jugel'
2 changes: 1 addition & 1 deletion demos/gtk3-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const file = GLib.path_get_basename(args[0])
const dir = GLib.path_get_dirname(args[0])
const root = GLib.path_get_dirname(dir)

const test_md = path(root, 'test.md')
const test_md = path(root, 'tests/test.md')
const readme_md = path(root, 'README.md')
const contrib_md = path(root, 'CONTRIBUTING.md')
const src = path(root, 'src')
Expand Down
2 changes: 1 addition & 1 deletion demos/gtk4-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const file = GLib.path_get_basename(args[0])
const dir = GLib.path_get_dirname(args[0])
const root = GLib.path_get_dirname(dir)

const test_md = path(root, 'test.md')
const test_md = path(root, 'tests/test.md')
const readme_md = path(root, 'README.md')
const contrib_md = path(root, 'CONTRIBUTING.md')
const src = path(root, 'src')
Expand Down
159 changes: 97 additions & 62 deletions src/bin/md2pango
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@ let sub_h1, sub_h2, sub_h3
// m2p_sections defines how to detect special markdown sections.
// These expressions scan the full line to detect headings, lists, and code.
const m2p_sections = [
sub_h1 = { name: H1, re: /^(#\s+)(.*)(\s*)$/, sub: "<big><big><big>$2</big></big></big>" },
sub_h2 = { name: H2, re: /^(##\s+)(.*)(\s*)$/, sub: "<big><big>$2</big></big>" },
sub_h1 = { name: H1, re: /^(#\s+)(.*)(\s*)$/, sub: "<big><big><big>$2</big></big></big>" },
sub_h2 = { name: H2, re: /^(##\s+)(.*)(\s*)$/, sub: "<big><big>$2</big></big>" },
sub_h3 = { name: H3, re: /^(###\s+)(.*)(\s*)$/, sub: "<big>$2</big>" },
{ name: UL, re: /^(\s*[\*\-]\s)(.*)(\s*)$/, sub: " • $2" },
{ name: UL, re: /^(\s*[\*\-]\s)(.*)(\s*)$/, sub: " • $2" },
{ name: OL, re: /^(\s*[0-9]+\.\s)(.*)(\s*)$/, sub: " $1$2" },
{ name: CODE, re: /^```[a-z_]*$/, sub: "<tt>" },
{ name: CODE, re: /^```[a-z_]*$/, sub: "<tt>" },
]

// m2p_styles defines how to replace inline styled text
const m2p_styles = [
{ name: BOLD, re: /(^|[^\*])(\*\*)(.*)(\*\*)/g, sub: "$1<b>$3</b>" },
{ name: BOLD, re: /(\*\*)(.*)(\*\*)([^\*]|$)/g, sub: "<b>$3</b>$4" },
{ name: EMPH, re: /(^|[^\*])(\*)(.*)(\*)/g, sub: "$1<i>$3</i>" },
{ name: EMPH, re: /(\*)(.*)(\*)([^\*]|$)/g, sub: "<i>$3</i>$4" },
{ name: PRE, re: /(`)([^`]*)(`)/g, sub: "<tt>$2</tt>" },
{ name: LINK, re: /(!)?(\[)(.*)(\]\()(.+)(\))/g, sub: "<a href='$5'>$3</a>" },
{ name: LINK, re: /(!)?(\[)(.*)(\]\(\))/g, sub: "<a href='$3'>$3</a>" },
{ name: EMPH, re: /(^|[^\*])(\*)(.*)(\*)/g, sub: "$1<i>$3</i>" },
{ name: EMPH, re: /(\*)(.*)(\*)([^\*]|$)/g, sub: "<i>$3</i>$4" },
{ name: PRE, re: /(`)([^`]*)(`)/g, sub: "<tt>$2</tt>" },
{ name: LINK, re: /(!)?(\[)(.*)(\]\()(.+)(\))/g, sub: "<a href='$5'>$3</a>" },
{ name: LINK, re: /(!)?(\[)(.*)(\]\(\))/g, sub: "<a href='$3'>$3</a>" },
]

const re_comment = /^\s*<!--.*-->\s*$/
Expand All @@ -45,7 +45,7 @@ const m2p_escapes = [
[/<!--.*-->/, ''],
[/&/g, '&amp;'],
[/</g, '&lt;'],
[/>/g, '&gt;'],
[/>/g, '&gt;'],
]

const code_color_span = "<span foreground='#bbb' background='#222'>"
Expand All @@ -59,115 +59,150 @@ const pad = (lines, start=1, end=1) => {

function convert(text) {
let lines = text.split('\n')
let code = false
let out = []
let pre = []

// Indicates if the current line is within a code block
let is_code = false
let code_lines = []

let output = []
let color_span_open = false
let tt_must_close = false

const try_close_span = () => {
if (color_span_open) {
out.push('</span>')
output.push('</span>')
color_span_open = false
}
}

const try_open_span = () => {
if (!color_span_open) {
out.push('</span>')
output.push('</span>')
color_span_open = false
}
}


for (const line of lines) {
// first parse color macros in non-code texts
if(!code) {
if (!is_code) {
let colors = line.match(re_color)
if (colors || line.match(re_reset)) try_close_span()
if (colors || line.match(re_reset)) {
try_close_span()
}

if (colors) {
try_close_span()
if(color_span_open) close_span()
if (color_span_open) {
close_span()
}

let fg = colors[2] == 'fg'? colors[3] : colors[5] == 'fg'? colors[6] : ''
let bg = colors[2] == 'bg'? colors[3] : colors[5] == 'bg'? colors[6] : ''
let attrs = ''
if(fg != '') { attrs += ` foreground='${fg}'`}
if(bg != '') { attrs += ` background='${bg}'`}
if (attrs != '') {
out.push(`<span${attrs}>`)

if (fg != '') {
attrs += ` foreground='${fg}'`
}

if (bg != '') {
attrs += ` background='${bg}'`
}

if (attrs != '') {
output.push(`<span${attrs}>`)
color_span_open = true
}
}
}

// all macros processed, lets remove remaining comments
if (line.match(re_comment)) continue
if (line.match(re_comment)) {
continue
}

// escape all non-verbatim text
let result = code? line : escape_line(line)
// is this line an opening statement of a code block
let code_start = false
let match = null
for (sec of m2p_sections) {
if (match = line.match(sec.re)) {
switch (sec.name) {
case CODE:
if (!code) {
code_start=true
if (color_span_open) {
// cannot color
result = '<tt>'
tt_must_close = false
} else {
result = code_color_span + '<tt>'
tt_must_close = true
}

// escape all non-verbatim text
let result = is_code ? line : escape_line(line)

for ({ re, sub, name } of m2p_sections) {
if (line.match(re)) {
if (name === CODE) {
if (!is_code) {
// haven't been inside a code block, so ``` indicates
// that it is starting now
code_start = true
is_code = true

if (color_span_open) {
// cannot color
result = '<tt>'
tt_must_close = false
} else {
result = code_color_span + '<tt>'
tt_must_close = true
}
else {
out.push(...pad(pre).map(escape_line))
result='</tt>'
if (tt_must_close) {
result += '</span>'
tt_must_close = false
}
} else {
// the code block ends now
is_code = false
output.push(...pad(code_lines).map(escape_line))
code_lines = []
result = '</tt>'
if (tt_must_close) {
result += '</span>'
tt_must_close = false
}
code=!code
break
default:
if (code) result = line
else result = line.replace(sec.re, sec.sub)
break
}
} else {
if (is_code) {
result = line
} else {
result = line.replace(re, sub)
}
}
break
}
}
if (code && !code_start) {
pre.push(result)

if (is_code && !code_start) {
code_lines.push(result)
continue
}

if (line.match(re_h1line)) {
out.push(`# ${out.pop()}`.replace(sub_h1.re, sub_h1.sub))
output.push(`# ${output.pop()}`.replace(sub_h1.re, sub_h1.sub))
continue
}

if (line.match(re_h2line)) {
out.push(`## ${out.pop()}`.replace(sub_h2.re, sub_h2.sub))
output.push(`## ${output.pop()}`.replace(sub_h2.re, sub_h2.sub))
continue
}

// all other text can be styled
for (const style of m2p_styles) {
result = result.replace(style.re, style.sub)
}

// all raw urls can be linked if possible
let uri = result.match(re_uri) // look for any URI
let uri = result.match(re_uri) // look for any URI
let href = result.match(re_href) // and for URIs in href=''
let atag = result.match(re_atag) // and for URIs in <a></a>
href = href && href[1] == uri
atag = href && atag[1] == uri
if (uri && (href || atag)) {
result = result.replace(uri, `<a href='${uri}'>${uri}</a>`)
}
out.push(result)

output.push(result)
}

try_close_span()
return out.join('\n')

// remove trailing whitespaces
output = output.map(line => line.replace(/ +$/, ''))

return output.join('\n')
}

const readFile = (f) => {
Expand Down
Loading