Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support p and pp for CommonMarker::Node #44

Merged
merged 1 commit into from
Apr 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/commonmarker/node.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
require 'commonmarker/node/inspect'

module CommonMarker
class Node
include Enumerable
include Inspect

# Public: An iterator that "walks the tree," descending into children recursively.
#
Expand Down
59 changes: 59 additions & 0 deletions lib/commonmarker/node/inspect.rb
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
8 changes: 8 additions & 0 deletions test/test_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,12 @@ def test_walk_and_delete_node
end
assert_equal "<p>Hi there, I am mostly text!</p>\n", @doc.to_html
end

def test_inspect
assert_match /#<CommonMarker::Node\(document\):/, @doc.inspect
end

def test_pretty_print
assert_match /#<CommonMarker::Node\(document\):/, PP.pp(@doc, '')
end
end