This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
/
filters.nim
74 lines (69 loc) · 2.28 KB
/
filters.nim
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
73
74
import
streams, strtabs, osproc,
packages / docutils / [rst, rstgen]
proc escape_html*(target: Stream, value: string,
escapeQuotes: bool = false) =
## translates the characters `&`, `<` and `>` to their corresponding
## HTML entities. if `escapeQuotes` is `true`, also translates
## `"` and `'`.
for c in value:
case c:
of '&': target.write("&")
of '<': target.write("<")
of '>': target.write(">")
of '"':
if escapeQuotes: target.write(""")
else: target.write('"')
of '\'':
if escapeQuotes: target.write("'")
else: target.write('\'')
else:
target.write(c)
proc change_indentation*(target: Stream, value: string,
indentation: string) =
var
in_indentation = false
initial = true
consumed_whitespace = false
for c in value:
case c
of '\l':
if initial:
initial = false
consumed_whitespace = true
if in_indentation:
target.write('\l')
else:
in_indentation = true
of ' ':
if initial:
consumed_whitespace = true
else:
if not in_indentation:
target.write(' ')
else:
if initial:
initial = false
if consumed_whitespace and not in_indentation:
target.write(' ');
if in_indentation:
target.write('\l' & indentation)
in_indentation = false
target.write(c)
proc rst*(target: Stream, value: string, options: RstParseOptions = {},
config: StringTableRef = newStringTable()) =
target.write(rstToHtml(value, options, config))
proc pygmentize*(target: Stream, value: string, language: string) =
var p = startProcess("pygmentize -l " & language & " -f html",
options={poEvalCommand})
var input = p.inputStream
var output = p.outputStream
input.write(value)
input.flush()
input.close()
discard p.waitForExit()
var c = output.readChar()
while c != char(0):
target.write(c)
c = output.readChar()
p.close()