-
-
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 #44 from gfx/inspect
support p and pp for CommonMarker::Node
- Loading branch information
Showing
3 changed files
with
70 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'pp' | ||
|
||
module CommonMarker | ||
class Node | ||
module Inspect | ||
PP_INDENT_SIZE = 2 | ||
|
||
def inspect | ||
PP.pp(self, String.new, Float::INFINITY) | ||
end | ||
|
||
# @param [PrettyPrint] pp | ||
def pretty_print(pp) | ||
pp.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}):", '>') do | ||
pp.breakable | ||
|
||
attrs = %i[ | ||
sourcepos | ||
string_content | ||
url | ||
title | ||
header_level | ||
list_type | ||
list_start | ||
list_tight | ||
fence_info | ||
].map do |name| | ||
begin | ||
[name, __send__(name)] | ||
rescue NodeError | ||
nil | ||
end | ||
end.compact | ||
|
||
pp.seplist(attrs) do |name, value| | ||
pp.text "#{name}=" | ||
pp.pp value | ||
end | ||
|
||
if first_child | ||
pp.breakable | ||
pp.group(PP_INDENT_SIZE) do | ||
children = [] | ||
node = first_child | ||
while node | ||
children << node | ||
node = node.next | ||
end | ||
pp.text 'children=' | ||
pp.pp children | ||
end | ||
end | ||
end | ||
end | ||
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