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

Request: PrettyPrint support for CommonMarker::Node #42

Closed
gfx opened this issue Apr 9, 2017 · 1 comment
Closed

Request: PrettyPrint support for CommonMarker::Node #42

gfx opened this issue Apr 9, 2017 · 1 comment

Comments

@gfx
Copy link
Contributor

gfx commented Apr 9, 2017

Debugging CommonMarker::Node is a pain because it doesn't support pp.

I have defined a monkey patch which is something like this:

class CommonMarker
  class Node
    # @param [PrettyPrint] pp
    def pretty_print(pp)
      pp.group(PP_INDENT_SIZE, "#<#{self.class}(#{type}): ", ">") do
        pp.breakable

        attrs = [
          :sourcepos,
          :string_content,
          :url,
          :title,
          :header_level,
          :list_type,
          :list_start,
          :list_tight,
          :fence_info, # the label for fenced code blocks
          :first_child,
        ].map do |name|
          [name, __send__(name)] rescue nil
        end.compact

        pp.seplist(attrs) do |name, value|
          if name != :first_child
            pp.text "#{name}="
            pp.pp value
          elsif value
            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

With this routine pp CommonMarker.render_doc("foo **bar** baz") results in:

#<CommonMarker::Node(document):
  sourcepos={:start_line=>1, :start_column=>1, :end_line=>1, :end_column=>15},
  children=[#<CommonMarker::Node(paragraph):
       sourcepos={:start_line=>1,
        :start_column=>1,
        :end_line=>1,
        :end_column=>15},
       children=[#<CommonMarker::Node(text):
            sourcepos={:start_line=>0,
             :start_column=>0,
             :end_line=>0,
             :end_column=>0},
            string_content="foo ",
            >,
          #<CommonMarker::Node(strong):
            sourcepos={:start_line=>0,
             :start_column=>0,
             :end_line=>0,
             :end_column=>0},
            children=[#<CommonMarker::Node(text):
                 sourcepos={:start_line=>0,
                  :start_column=>0,
                  :end_line=>0,
                  :end_column=>0},
                 string_content="bar",
                 >]>,
          #<CommonMarker::Node(text):
            sourcepos={:start_line=>0,
             :start_column=>0,
             :end_line=>0,
             :end_column=>0},
            string_content=" baz",
            >]>]>
@gfx gfx changed the title Request: PrettyPrint support Request: PrettyPrint support for CommonMarker::Node Apr 9, 2017
@gjtorikian
Copy link
Owner

I'm afraid I don't know much about extending support for pretty print. Your code looks good to me, would you mind submitting a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants