From aebf7308ef00a8c2150200dc641d3eeee80a8303 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 22 Nov 2014 08:39:49 -0800 Subject: Added examples of using walk. --- commonmark.rb | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'commonmark.rb') diff --git a/commonmark.rb b/commonmark.rb index dca7a3c..b41b8fe 100755 --- a/commonmark.rb +++ b/commonmark.rb @@ -335,10 +335,22 @@ class HtmlRenderer < Renderer end doc = Node.parse_file(ARGF) + +# Walk tree and print URLs for links doc.walk do |node| if node.type == :link printf("URL = %s\n", node.url) - printf("parent is %s\n", node.parent.type) + end +end + +# Walk tree and transform links to regular text +doc.walk do |node| + if node.type == :link + parent = node.parent + index = parent.children.index(node) + len = parent.children.length + parent.children.replace(parent.children.slice(0,index) + node.children + + parent.children.slice(index + 1, len)) end end -- cgit v1.2.3