/
block2hn.js
executable file
·72 lines (60 loc) · 1.61 KB
/
block2hn.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env node
if (process.argv.length != 3) {
console.log("Usage: block2hn <textpath>")
console.log(process.argv.length)
process.exit(1)
}
const path = require('path')
const fs = require('fs')
const {encode} = require('html-entities')
const filepath = process.argv[2]
const cont = fs.readFileSync( filepath )
const srcLines = cont.toString().split('\n')
class Paragraph {
constructor(begin, end, text) {
this.begin = begin
this.end = end
this.text = text
}
}
const linesToParas = (lines) => {
let begin = 0
let paras = []
let curlines = []
for( let [idx, line] of lines.entries())
{
if (line == "")
{
paras.push( new Paragraph(begin, idx, curlines.join('<br>\n')))
begin = idx+1
curlines = []
}
else
{
curlines.push(encode(line))
}
}
if (curlines.length != 0)
{
paras.push( new Paragraph(begin, lines.length, curlines.join('<br>\n')))
}
return paras
}
const paras = linesToParas(srcLines)
// <div class="hn-multi-sel box" hn-value="1,2">Hello</div>
const para2html = (para) => {
const beg = Math.max(1, para.begin)
return `<div class="hn-multi-sel box" hn-value="${beg},${para.end}">
${para.text}
</div>`
}
const paras2html = (paras) => {
return paras.map( p => para2html(p) ).join("\n")
}
const parahtml = paras2html(paras)
console.log(parahtml)
console.log(`<div class="buttons level-right">
<button class="button hn-cancel">Cancel</button>
<button class="button hn-submit">Archive</button>
</div>
`)