Module: MetaCommit::Extension::MarkdownSupport::ContextualAstAccessor
- Included in:
- Diffs::Diff
- Defined in:
- lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb
Constant Summary collapse
- ELEMENT_TYPE_ANCHOR =
:link
- ELEMENT_TYPE_LISTS =
:list
- ELEMENT_TYPE_LIST_ITEM =
:list_item
- ELEMENT_TYPE_HEADER =
:header
- ELEMENT_TYPE_CODE =
:code_block
- ELEMENT_TYPE_TEXT =
:text
- ELEMENT_TYPE_PARAGRAPH =
:paragraph
- ELEMENT_TYPE_IMAGE =
:image
Instance Method Summary collapse
- #anchor?(ast) ⇒ Boolean
- #anchor_context?(contextual_ast) ⇒ Boolean
- #anchor_target(contextual_ast) ⇒ String
- #anchor_title(contextual_ast) ⇒ String
- #closest_header_of_list(contextual_ast) ⇒ String
- #code?(ast) ⇒ Boolean
- #code_context?(contextual_ast) ⇒ String
- #code_first_line(contextual_ast) ⇒ String
-
#context_node_neighbours(contextual_ast, depth = 1) ⇒ MetaCommit::Extension::MarkdownSupport::Model::Ast, nil (?)
returns ast node that goes before contextual_ast.target_node contextual_ast.context_nodes are filled using depth-first search this method returns value using breadth-first search.
- #context_nodes_on_line(contextual_ast, line) ⇒ Array<MetaCommit::Model::Node>
- #contextual_ast_has_target_node(ast) ⇒ Boolean
- #element_closest_to(nodes, column) ⇒ Boolean, Nil
- #elements_of_type_on_line(contextual_ast, type, line) ⇒ Boolean, Nil
- #header?(ast) ⇒ Boolean
- #header_content(ast) ⇒ String
- #image?(ast) ⇒ Boolean
- #list?(ast) ⇒ Boolean
- #list_context?(contextual_ast) ⇒ Boolean
- #list_item?(ast) ⇒ Boolean
- #nested_list_context?(contextual_ast) ⇒ Boolean
- #paragraph?(ast) ⇒ Boolean
- #paragraph_context?(contextual_ast) ⇒ String
- #parent_list_item_title(contextual_ast) ⇒ String
- #starts_on_line?(ast, line) ⇒ Boolean
- #string_content(ast) ⇒ String
- #text?(ast) ⇒ Boolean
- #text_context?(contextual_ast) ⇒ String
Instance Method Details
#anchor?(ast) ⇒ Boolean
37 38 39 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 37 def anchor?(ast) ast.element.type == ELEMENT_TYPE_ANCHOR end |
#anchor_context?(contextual_ast) ⇒ Boolean
32 33 34 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 32 def anchor_context?(contextual_ast) contextual_ast.context_nodes.any? {|node| anchor?(node)} end |
#anchor_target(contextual_ast) ⇒ String
51 52 53 54 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 51 def anchor_target(contextual_ast) anchor = contextual_ast.context_nodes.find {|node| anchor?(node)} anchor.element.url end |
#anchor_title(contextual_ast) ⇒ String
43 44 45 46 47 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 43 def anchor_title(contextual_ast) anchor = contextual_ast.context_nodes.find {|node| anchor?(node)} return anchor.element.first_child.first_child.string_content unless text?(anchor.children.first) anchor.element.first_child.string_content end |
#closest_header_of_list(contextual_ast) ⇒ String
92 93 94 95 96 97 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 92 def closest_header_of_list(contextual_ast) sibling_neighbours = context_node_neighbours(contextual_ast) closest_header = sibling_neighbours.reverse.find {|neighbour| header?(neighbour)} closest_header.children.first.element.string_content unless closest_header.nil? end |
#code?(ast) ⇒ Boolean
107 108 109 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 107 def code?(ast) ast.element.type == ELEMENT_TYPE_CODE end |
#code_context?(contextual_ast) ⇒ String
101 102 103 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 101 def code_context?(contextual_ast) contextual_ast.context_nodes.any? {|node| code?(node)} end |
#code_first_line(contextual_ast) ⇒ String
113 114 115 116 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 113 def code_first_line(contextual_ast) codespan = contextual_ast.context_nodes.reverse.find {|node| code?(node)} codespan.element.fence_info end |
#context_node_neighbours(contextual_ast, depth = 1) ⇒ MetaCommit::Extension::MarkdownSupport::Model::Ast, nil (?)
returns ast node that goes before contextual_ast.target_node contextual_ast.context_nodes are filled using depth-first search this method returns value using breadth-first search
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 160 def context_node_neighbours(contextual_ast, depth=1) # TODO depth can be root target_node_sibling = contextual_ast.context_nodes[depth] node_to_be_searched = contextual_ast.context_nodes[depth - 1] previous_children = [] node_to_be_searched.children.map do |child| break if child == target_node_sibling previous_children << child end previous_children end |
#context_nodes_on_line(contextual_ast, line) ⇒ Array<MetaCommit::Model::Node>
188 189 190 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 188 def context_nodes_on_line(contextual_ast, line) contextual_ast.context_nodes.select {|node| starts_on_line?(node, line)} end |
#contextual_ast_has_target_node(ast) ⇒ Boolean
174 175 176 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 174 def contextual_ast_has_target_node(ast) !ast.target_node.nil? end |
#element_closest_to(nodes, column) ⇒ Boolean, Nil
24 25 26 27 28 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 24 def element_closest_to(nodes, column) nodes .select {|node| column.nil? || node.first_column.nil? || node.last_column.nil? || node.first_column <= column} .last end |
#elements_of_type_on_line(contextual_ast, type, line) ⇒ Boolean, Nil
16 17 18 19 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 16 def elements_of_type_on_line(contextual_ast, type, line) context_nodes_on_line(contextual_ast, line) .select {|node| node.element.type == type} end |
#header?(ast) ⇒ Boolean
144 145 146 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 144 def header?(ast) ast.element.type == ELEMENT_TYPE_HEADER end |
#header_content(ast) ⇒ String
150 151 152 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 150 def header_content(ast) ast.children.first.element.string_content end |
#image?(ast) ⇒ Boolean
194 195 196 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 194 def image?(ast) ast.element.type == ELEMENT_TYPE_IMAGE end |
#list?(ast) ⇒ Boolean
80 81 82 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 80 def list?(ast) ast.element.type == ELEMENT_TYPE_LISTS end |
#list_context?(contextual_ast) ⇒ Boolean
58 59 60 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 58 def list_context?(contextual_ast) contextual_ast.context_nodes.any? {|node| list?(node)} end |
#list_item?(ast) ⇒ Boolean
86 87 88 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 86 def list_item?(ast) ast.element.type == ELEMENT_TYPE_LIST_ITEM end |
#nested_list_context?(contextual_ast) ⇒ Boolean
64 65 66 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 64 def nested_list_context?(contextual_ast) contextual_ast.context_nodes.count {|node| list?(node)} > 1 end |
#paragraph?(ast) ⇒ Boolean
138 139 140 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 138 def paragraph?(ast) ast.element.type == ELEMENT_TYPE_PARAGRAPH end |
#paragraph_context?(contextual_ast) ⇒ String
132 133 134 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 132 def paragraph_context?(contextual_ast) contextual_ast.context_nodes.any? {|node| paragraph?(node)} end |
#parent_list_item_title(contextual_ast) ⇒ String
70 71 72 73 74 75 76 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 70 def parent_list_item_title(contextual_ast) parent_element = contextual_ast .context_nodes.reverse # walk from closest node to border .select {|node| list_item?(node)}[1] # li elements .children.first.children.first string_content(parent_element) end |
#starts_on_line?(ast, line) ⇒ Boolean
181 182 183 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 181 def starts_on_line?(ast, line) ast.first_line === line end |
#string_content(ast) ⇒ String
200 201 202 203 204 205 206 207 208 209 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 200 def string_content(ast) case ast.element.type when ELEMENT_TYPE_ANCHOR ast.children.first.element.string_content when ELEMENT_TYPE_LIST_ITEM ast.children.first.children.first.element.string_content else ast.element.string_content end end |
#text?(ast) ⇒ Boolean
126 127 128 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 126 def text?(ast) ast.element.type == ELEMENT_TYPE_TEXT end |
#text_context?(contextual_ast) ⇒ String
120 121 122 |
# File 'lib/meta_commit_markdown_support/helpers/contextual_ast_accessor.rb', line 120 def text_context?(contextual_ast) contextual_ast.context_nodes.any? {|node| text?(node)} end |