-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathdemo.rb
64 lines (55 loc) · 1.31 KB
/
demo.rb
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
require 'commonmarker'
# parse the files specified on the command line
doc = CommonMarker::Node.parse_file(ARGF)
# Walk tree and print out URLs for links
doc.walk do |node|
if node.type == :link
printf("URL = %s\n", node.url)
end
end
# Capitalize all regular text in headers
doc.walk do |node|
if node.type == :header
node.walk do |subnode|
if subnode.type == :text
subnode.string_content = subnode.string_content.upcase
end
end
end
end
# Transform links to regular text
doc.walk do |node|
if node.type == :link
node.each_child do |child|
node.insert_before(child)
end
node.delete
end
end
# Render the transformed document to a string
print(doc.to_html)
# Create a custom renderer.
class MyHtmlRenderer < CommonMarker::HtmlRenderer
def initialize
super
@headerid = 1
end
def header(node)
block do
self.out("<h", node.header_level, " id=\"", @headerid, "\">",
:children, "</h", node.header_level, ">")
@headerid += 1
end
end
end
# this renderer prints directly to STDOUT, instead
# of returning a string
myrenderer = MyHtmlRenderer.new
print(myrenderer.render(doc))
# Print any warnings to STDERR
myrenderer.warnings.each do |w|
STDERR.write(w)
STDERR.write("\n")
end
# free allocated memory when you're done
doc.free