-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from gjtorikian/enumerate
Make `Node` an `Enumerable`
- Loading branch information
Showing
5 changed files
with
88 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module CommonMarker | ||
class Node | ||
include Enumerable | ||
|
||
# Public: An iterator that "walks the tree," descending into children recursively. | ||
# | ||
# blk - A {Proc} representing the action to take for each child | ||
def walk(&block) | ||
return enum_for(:walk) unless block_given? | ||
|
||
yield self | ||
each do |child| | ||
child.walk(&block) | ||
end | ||
end | ||
|
||
# Public: Convert the node to an HTML string. | ||
# | ||
# options - A {Symbol} or {Array of Symbol}s indicating the render options | ||
# | ||
# Returns a {String}. | ||
def to_html(options = :default) | ||
opts = Config.process_options(options, :render) | ||
_render_html(opts).force_encoding('utf-8') | ||
end | ||
|
||
# Public: Iterate over the children (if any) of the current pointer. | ||
def each(&block) | ||
return enum_for(:each) unless block_given? | ||
|
||
child = first_child | ||
while child | ||
nextchild = child.next | ||
yield child | ||
child = nextchild | ||
end | ||
end | ||
|
||
# Deprecated: Please use `each` instead | ||
def each_child(&block) | ||
warn '[DEPRECATION] `each_child` is deprecated. Please use `each` instead.' | ||
each(&block) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters