diff options
author | John MacFarlane <jgm@berkeley.edu> | 2014-11-22 08:39:49 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2014-11-22 08:39:49 -0800 |
commit | aebf7308ef00a8c2150200dc641d3eeee80a8303 (patch) | |
tree | 343708d49a13f20187d780821390c2ae90144998 /commonmark.rb | |
parent | 82e759174bc8fcd8fd070a8b379adcdaa048e7cd (diff) |
Added examples of using walk.
Diffstat (limited to 'commonmark.rb')
-rwxr-xr-x | commonmark.rb | 14 |
1 files changed, 13 insertions, 1 deletions
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 |