Class: Jekyll::WikiLinks::Parser
- Inherits:
-
Object
- Object
- Jekyll::WikiLinks::Parser
- Includes:
- Filters::URLFilters
- Defined in:
- lib/jekyll-wikilinks/util/parser.rb
Overview
more of a “parser” than a parser
Constant Summary collapse
- CONVERTER_CLASS =
Jekyll::Converters::Markdown
Instance Attribute Summary collapse
-
#doc_manager ⇒ Object
Returns the value of attribute doc_manager.
-
#markdown_converter ⇒ Object
Returns the value of attribute markdown_converter.
-
#wikilink_blocks ⇒ Object
Returns the value of attribute wikilink_blocks.
-
#wikilink_inlines ⇒ Object
Returns the value of attribute wikilink_inlines.
Instance Method Summary collapse
- #build_html(wikilink) ⇒ Object
-
#build_html_embed(title, content, url) ⇒ Object
building/converting.
- #build_html_img_embed(static_doc, is_svg = false) ⇒ Object
-
#initialize(site) ⇒ Parser
constructor
A new instance of Parser.
-
#parse(doc_filename, doc_content) ⇒ Object
parsing.
- #parse_blocks(doc_filename, doc_content) ⇒ Object
- #parse_inlines(doc_filename, doc_content) ⇒ Object
-
#sort_typed_first ⇒ Object
helpers.
Constructor Details
#initialize(site) ⇒ Parser
Returns a new instance of Parser.
17 18 19 20 21 22 23 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 17 def initialize(site) @context ||= Jekyll::WikiLinks::Context.new(site) # do not use @dm in parser -- it is only meant to be passed down into wikilink classes. @doc_manager ||= site.doc_mngr @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS) @wikilink_blocks, @wikilink_inlines = [], [] end |
Instance Attribute Details
#doc_manager ⇒ Object
Returns the value of attribute doc_manager.
10 11 12 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 10 def doc_manager @doc_manager end |
#markdown_converter ⇒ Object
Returns the value of attribute markdown_converter.
10 11 12 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 10 def markdown_converter @markdown_converter end |
#wikilink_blocks ⇒ Object
Returns the value of attribute wikilink_blocks.
10 11 12 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 10 def wikilink_blocks @wikilink_blocks end |
#wikilink_inlines ⇒ Object
Returns the value of attribute wikilink_inlines.
10 11 12 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 10 def wikilink_inlines @wikilink_inlines end |
Instance Method Details
#build_html(wikilink) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 119 def build_html(wikilink) if !wikilink.is_valid? return '<span class="' + $wiki_conf.css_name("invalid_wiki") + '">' + wikilink.md_str + '</span>' end # image processing if wikilink. && wikilink.is_img? return (wikilink.linked_img, is_svg=wikilink.is_img_svg?) end # markdown file processing linked_doc = wikilink.linked_doc link_type_txt = wikilink.is_typed? ? " #{$wiki_conf.css_name("typed")} #{wikilink.link_type}" : "" lnk_doc_rel_url = relative_url(linked_doc.url) if wikilink.labelled? inner_txt = wikilink.label_txt elsif linked_doc.data.keys.include?('title') inner_txt = linked_doc.data['title'].downcase # in case there is no 'title' frontmatter attribute # (i'm seeing deprecation warnings, but there might # be bugs caused by not using this...) elsif linked_doc.respond_to?(:title) inner_txt = linked_doc.title.downcase # pages don't have automatically generated titles else inner_txt = Jekyll::Utils.slugify(linked_doc.basename) end # level-specific if (wikilink.level == "file_path" || wikilink.level == "filename") return ( linked_doc.title, linked_doc.content, lnk_doc_rel_url ) if wikilink. elsif (wikilink.level == "header") # from: https://github.com/jekyll/jekyll/blob/6855200ebda6c0e33f487da69e4e02ec3d8286b7/Rakefile#L74 lnk_doc_rel_url += "\#" + Jekyll::Utils.slugify(wikilink.header_txt) inner_txt += " > #{wikilink.header_txt.downcase}" if !wikilink.labelled? elsif (wikilink.level == "block") lnk_doc_rel_url += "\#" + wikilink.block_id inner_txt += " > ^#{wikilink.block_id}" if !wikilink.labelled? else Jekyll.logger.error("Jekyll-Wikilinks: Invalid wikilink level") end return '<a class="' + $wiki_conf.css_name("wiki") + link_type_txt + '" href="' + lnk_doc_rel_url + '">' + inner_txt + '</a>' end |
#build_html_embed(title, content, url) ⇒ Object
building/converting
92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 92 def (title, content, url) # multi-line for readability return [ "<div class=\"#{$wiki_conf.css_name("embed_wrapper")}\">", "<div class=\"#{$wiki_conf.css_name("embed_title")}\">", "#{title}", "</div>", "<div class=\"#{$wiki_conf.css_name("embed_content")}\">", "#{@markdown_converter.convert(content)}", "</div>", "<a class=\"#{$wiki_conf.css_name("embed_wiki_link")}\" href=\"#{url}\"></a>", "</div>", ].join("\n").gsub!("\n", "") end |
#build_html_img_embed(static_doc, is_svg = false) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 107 def (static_doc, is_svg=false) svg_content = "" if is_svg File.open(static_doc.path, "r") do |svg_img| svg_content = svg_img.read end return "<p><span class=\"#{$wiki_conf.css_name("embed_image_wrapper")}\">#{svg_content}</span></p>" else return "<p><span class=\"#{$wiki_conf.css_name("embed_image_wrapper")}\"><img class=\"#{$wiki_conf.css_name("embed_image")}\" src=\"#{relative_url(static_doc.relative_path)}\"></span></p>" end end |
#parse(doc_filename, doc_content) ⇒ Object
parsing
27 28 29 30 31 32 33 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 27 def parse(doc_filename, doc_content) @wikilink_blocks, @wikilink_inlines = [], [] if !$wiki_conf.disabled_attributes? self.parse_blocks(doc_filename, doc_content) end self.parse_inlines(doc_filename, doc_content) end |
#parse_blocks(doc_filename, doc_content) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 35 def parse_blocks(doc_filename, doc_content) block_matches = doc_content.scan(REGEX_WIKI_LINK_BLOCKS) if !block_matches.nil? && block_matches.size != 0 block_matches.each do |wl_match| # init block wikilink wikilink_block = WikiLinkBlock.new( @doc_manager, doc_filename, wl_match[0], # link_type wl_match[2], # bullet_type ) # extract + add filenames items = wl_match[1] filename_matches = items.scan(/#{REGEX_LINK_LEFT}#{REGEX_FILENAME}#{REGEX_LINK_RIGHT}/i) filename_matches.each do |match| match.each do |fname| wikilink_block.add_item(fname) end end # replace text doc_content.gsub!(wikilink_block.md_regex, "\n") @wikilink_blocks << wikilink_block end end end |
#parse_inlines(doc_filename, doc_content) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 61 def parse_inlines(doc_filename, doc_content) inline_matches = doc_content.scan(REGEX_WIKI_LINK_INLINES) if !inline_matches.nil? && inline_matches.size != 0 inline_matches.each do |wl_match| @wikilink_inlines << WikiLinkInline.new( @doc_manager, doc_filename, wl_match[0], wl_match[1], wl_match[2], wl_match[3], wl_match[4], wl_match[5], ) end end # replace text return if @wikilink_inlines.nil? # process typed wikilinks first so we don't accidentally # overwrite them when handling untyped wikilinks self.sort_typed_first @wikilink_inlines.each do |wikilink| doc_content.gsub!( wikilink.md_regex, self.build_html(wikilink) ) end end |
#sort_typed_first ⇒ Object
helpers
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/jekyll-wikilinks/util/parser.rb', line 166 def sort_typed_first # sorting inline wikilinks is necessary so when wikilinks are replaced, # longer strings are replaced first so as not to accidentally overwrite # substrings # (this is especially likely if there is a matching wikilink that # appears as both untyped and typed in a document) temp = @wikilink_inlines.dup @wikilink_inlines.clear() typed_wikilinks = temp.select { |wl| wl.is_typed? } untyped_wikilinks = temp.select { |wl| !wl.is_typed? } @wikilink_inlines = typed_wikilinks.concat(untyped_wikilinks) end |