class Beardown::Document
Constants
- REGEXP_ASSET
- REGEXP_BLANKLINE
- REGEXP_BOLD
- REGEXP_CODEBLOCK
- REGEXP_CODESPAN
- REGEXP_HASHTAG
- REGEXP_HASHTAG_FULL
- REGEXP_HASHTAG_FULL_NOSPACE
- REGEXP_HEAD
- REGEXP_ITALIC
- REGEXP_LINE_SEPARATOR
- REGEXP_LINKPOST
- REGEXP_LINKURL
START_CHAR_LINKURL = “[” # duplicate with LINKPOST
- REGEXP_LIST_ORDERED
- REGEXP_LIST_TODO
- REGEXP_LIST_UNORDERED
- REGEXP_MARK
- REGEXP_MARK_NOSPACE
- REGEXP_P
- REGEXP_QUOTE
- REGEXP_STRIKE
- REGEXP_UNDERLINE
- START_CHAR_BOLD
- START_CHAR_CODESPAN
- START_CHAR_HASHTAG
- START_CHAR_ITALIC
- START_CHAR_LINKPOST
- START_CHAR_MARK
- START_CHAR_STRIKE
- START_CHAR_UNDERLINE
Public Class Methods
new(text)
click to toggle source
# File lib/beardown/document.rb, line 6 def initialize(text) @text = text.gsub(/(?<!^)>/, ">").gsub("<", "<") # These orders are important @blocks = [:head, :line_separator, :list_unordered, :list_ordered, :quote, :list_todo, :codeblock, :asset, :blankline, :p] @spans = [:codespan, :hashtag_full, :hashtag_full_nospace, :hashtag, :linkpost, :linkurl, :bold, :italic, :underline, :strike, :mark_nospace, :mark] @extensions = [] @title = String.new @tags = Array.new html end
Public Instance Methods
add_extension_todo_svg()
click to toggle source
# File lib/beardown/blocks/list_todo.rb, line 25 def add_extension_todo_svg <<~HEREDOC <svg display="none" version=1.1 xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <defs> <symbol id="todobox-uncheck"><g fill="none" fill-rule="evenodd"> <path d="M.5 12.853c0 1.462 1.185 2.647 2.647 2.647h9.706c1.462 0 2.647-1.185 2.647-2.647V3.147C15.5 1.685 14.315.5 12.853.5H3.147C1.685.5.5 1.685.5 3.147v9.706z" fill="#FFF"/> <path d="M.5 12.853c0 1.462 1.185 2.647 2.647 2.647h9.706c1.462 0 2.647-1.185 2.647-2.647V3.147C15.5 1.685 14.315.5 12.853.5H3.147C1.685.5.5 1.685.5 3.147v9.706z" stroke="#B4B4B4"/></g> </symbol> <symbol id="todobox-checked"><g fill="none" fill-rule="evenodd"> <path d="M.5 12.853c0 1.462 1.185 2.647 2.647 2.647h9.706c1.462 0 2.647-1.185 2.647-2.647V3.147C15.5 1.685 14.315.5 12.853.5H3.147C1.685.5.5 1.685.5 3.147v9.706z" fill="#FFF"/> <path d="M.5 12.853c0 1.462 1.185 2.647 2.647 2.647h9.706c1.462 0 2.647-1.185 2.647-2.647V3.147C15.5 1.685 14.315.5 12.853.5H3.147C1.685.5.5 1.685.5 3.147v9.706z" stroke="#B4B4B4"/> <path d="M12.526 4.615L6.636 9.58l-2.482-.836c-.19-.06-.408.003-.518.15-.116.15-.106.352.026.495l2.722 2.91c.086.09.21.144.34.144h.046c.12-.013.234-.07.307-.156l6.1-7.125c.143-.166.123-.407-.046-.548-.164-.138-.435-.14-.604 0z" fill="#555"/></g></symbol> </defs> </svg> HEREDOC end
check_color(tag)
click to toggle source
# File lib/beardown/spans/hashtag.rb, line 37 def check_color(tag) return false if(tag.length!=6) return tag.match? /[0-9a-fA-F]{6}/ end
content()
click to toggle source
def assets return @assets unless defined?(@assets).nil?
end
# File lib/beardown/document.rb, line 34 def content return @content if defined? @content @content = @text.delete "\n", "\t", "\s", "#", "*", "`", "*", "-", "_", "=", "+", "[", "]", ":", "<", ">" end
convert_asset(filename)
click to toggle source
# File lib/beardown/blocks/asset.rb, line 10 def convert_asset(filename) if is_img? filename return %(<p><img src="assets/#{filename}"></p>\n) else return %(<p><a href="assets/#{filename}">#{filename}</a></p>\n) end end
convert_blankline()
click to toggle source
# File lib/beardown/blocks/blankline.rb, line 9 def convert_blankline "<br>\n" end
convert_codeblock(content, code)
click to toggle source
# File lib/beardown/blocks/codeblock.rb, line 12 def convert_codeblock(content, code) lexer_class = case code when nil Rouge::Lexer.guess_by_source content #Rouge::Lexers::PlainText else Rouge::Lexers.const_get code end lexer = lexer_class.new formatter = Rouge::Formatters::HTML.new %(<pre><code class="code-multiline highlight">#{formatter.format(lexer.lex(content))}</code></pre>\n) end
convert_codespan(code)
click to toggle source
# File lib/beardown/spans/codespan.rb, line 11 def convert_codespan(code) %(<code class="code-inline">#{code}</code>) end
convert_hashcolor(hex)
click to toggle source
# File lib/beardown/spans/hashtag.rb, line 42 def convert_hashcolor(hex) %(<span class='color-preview' style='background-color:##{hex}'></span><span class='color-code'><span class='color-hash'>#</span>#{hex}</span></span>) end
convert_hashtag(tag)
click to toggle source
# File lib/beardown/spans/hashtag.rb, line 31 def convert_hashtag(tag) tag = tag.downcase render_hashtag tag %(<a href="../tag/#{tag}/" class="hashtag">##{tag}</a>) end
convert_head(id,content)
click to toggle source
# File lib/beardown/blocks/head.rb, line 16 def convert_head(id,content) "<h#{id}>#{content}</h#{id}>\n" end
convert_linkpost(title)
click to toggle source
# File lib/beardown/spans/linkpost.rb, line 14 def convert_linkpost(title) %(<a href="../#{title}/">#{title}</a>) end
convert_linkurl(title,url)
click to toggle source
# File lib/beardown/spans/linkurl.rb, line 11 def convert_linkurl(title,url) %(<a href="#{url}">#{title}</a>) end
convert_list_ordered(t_count, start_num, content)
click to toggle source
# File lib/beardown/blocks/list_ordered.rb, line 11 def convert_list_ordered(t_count, start_num, content) "<ol>" * t_count + %(<ol start="#{start_num}" style="counter-reset:ol #{start_num-1};"><li>) + scan_spans(StringScanner.new(content)) + "</li></ol>" + "</ol>" * t_count + "\n" end
convert_list_todo(t_count, check_status, content)
click to toggle source
# File lib/beardown/blocks/list_todo.rb, line 19 def convert_list_todo(t_count, check_status, content) ul_open = %(<ul class='todo-list #{check_status}'><li><svg width="16" height="16" viewBox="0 0 16 16"><use xlink:href="#todobox-#{check_status}"></use></svg>) ul_close = %(</li></ul>) "<ul>" * t_count + ul_open + scan_spans(StringScanner.new(content)) + ul_close + "</ul>" * t_count + "\n" end
convert_list_unordered(t_count, content)
click to toggle source
# File lib/beardown/blocks/list_unordered.rb, line 10 def convert_list_unordered(t_count, content) "<ul>" * t_count + "<ul><li>" + scan_spans(StringScanner.new(content)) + "</li></ul>" + "</ul>" * t_count + "\n" end
convert_p(indent, content)
click to toggle source
# File lib/beardown/blocks/p.rb, line 11 def convert_p(indent, content) "<p>#{" "*indent}#{scan_spans(StringScanner.new(content))}</p>\n" end
convert_quote(content)
click to toggle source
# File lib/beardown/blocks/quote.rb, line 10 def convert_quote(content) "<blockquote>#{scan_spans(StringScanner.new(content))}</blockquote>\n" end
html()
click to toggle source
# File lib/beardown/document.rb, line 45 def html return @html if defined? @html to_html end
is_img?(filename)
click to toggle source
# File lib/beardown/blocks/asset.rb, line 18 def is_img?(filename) ext = File.extname filename case ext.downcase when ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".ico", ".tiff", ".svg", ".apng" return true else return false end end
render_hashtag(tag)
click to toggle source
# File lib/beardown/spans/hashtag.rb, line 46 def render_hashtag(tag) tag = tag.downcase tags = tag.split "/" _tag = tags[0] @tags << _tag unless @tags.include? _tag tags[1..-1].each do |t| _tag += "/#{t}" @tags << _tag unless @tags.include? _tag end end
render_head(id, content)
click to toggle source
# File lib/beardown/blocks/head.rb, line 12 def render_head(id, content) @title << content if id==1&&@title.empty? end
scan_asset(s)
click to toggle source
# File lib/beardown/blocks/asset.rb, line 5 def scan_asset(s) filename = s[1] convert_asset filename end
scan_blankline(s)
click to toggle source
# File lib/beardown/blocks/blankline.rb, line 5 def scan_blankline(s) convert_blankline end
scan_codeblock(s)
click to toggle source
# File lib/beardown/blocks/codeblock.rb, line 6 def scan_codeblock(s) code = $code_map[s[1].strip.downcase] content = s[2] convert_codeblock(content, code) end
scan_codespan(s)
click to toggle source
# File lib/beardown/spans/codespan.rb, line 6 def scan_codespan(s) code = s[1] convert_codespan code end
scan_hashtag(s)
click to toggle source
# File lib/beardown/spans/hashtag.rb, line 22 def scan_hashtag(s) tag = s[1] if check_color(tag) convert_hashcolor tag else convert_hashtag tag end end
scan_head(s)
click to toggle source
# File lib/beardown/blocks/head.rb, line 5 def scan_head(s) id = s[1].length content = s[2] render_head id, content convert_head id, content end
scan_line_separator(s)
click to toggle source
# File lib/beardown/blocks/line_separator.rb, line 5 def scan_line_separator(s) "<hr>\n" end
scan_linkpost(s)
click to toggle source
I use this not as a X callback url Scheme Instead, A url to this website used in Miyano Blog system
# File lib/beardown/spans/linkpost.rb, line 9 def scan_linkpost(s) title = s[1] convert_linkpost(title) end
scan_linkurl(s)
click to toggle source
# File lib/beardown/spans/linkurl.rb, line 6 def scan_linkurl(s) title, url = s[1], s[2] convert_linkurl(title, url) end
scan_list_ordered(s)
click to toggle source
# File lib/beardown/blocks/list_ordered.rb, line 4 def scan_list_ordered(s) t_count = s[1].length start_num = s[2].to_i content = s[3] convert_list_ordered(t_count, start_num, content) end
scan_list_todo(s)
click to toggle source
# File lib/beardown/blocks/list_todo.rb, line 5 def scan_list_todo(s) t_count = s[1].length check_status = case s[2] when "-" then "uncheck" when "+" then "checked" end content = s[3] ex_svg = :add_extension_todo_svg @extensions << ex_svg unless @extensions.include? ex_svg convert_list_todo(t_count, check_status, content) end
scan_list_unordered(s)
click to toggle source
# File lib/beardown/blocks/list_unordered.rb, line 4 def scan_list_unordered(s) t_count = s[1].length content = s[2] convert_list_unordered(t_count, content) end
scan_p(s)
click to toggle source
# File lib/beardown/blocks/p.rb, line 5 def scan_p(s) indent = s[1].length content = s[2] convert_p indent, content end
scan_quote(s)
click to toggle source
# File lib/beardown/blocks/quote.rb, line 5 def scan_quote(s) content = s[1] convert_quote(content) end
summary()
click to toggle source
# File lib/beardown/document.rb, line 39 def summary return @summary if defined? @summary s = content @summary = s[0...30] end
title()
click to toggle source
# File lib/beardown/document.rb, line 22 def title @title end
to_html()
click to toggle source
# File lib/beardown/document.rb, line 50 def to_html return @html if defined? @html s = StringScanner.new @text res = String.new while !s.eos? do res << scan_blocks(s) end res << add_extensions @html = res end
Protected Instance Methods
add_extensions()
click to toggle source
# File lib/beardown/document.rb, line 115 def add_extensions res = String.new @extensions.each do |ex| res << send(ex) end res end
regexp_span_like()
click to toggle source
# File lib/beardown/document.rb, line 76 def regexp_span_like return @regexp_span_like if defined? @regexp_span_like res = String.new @spans.each do |e| begin start_char = Document.const_get("START_CHAR_#{e.to_s.upcase}") res << "\\#{start_char}" rescue NameError next end end @regexp_span_like = /[#{res}]/ end
scan_blocks(s)
click to toggle source
# File lib/beardown/document.rb, line 63 def scan_blocks(s) @blocks.each do |e| #begin unless s.scan(Document.const_get("REGEXP_#{e.to_s.upcase}")).nil? return send "scan_#{e.to_s}", s end #rescue NameError # next #end end return s.string end
scan_spans(s)
click to toggle source
# File lib/beardown/document.rb, line 90 def scan_spans(s) while !s.eos? last_pos = s.pos like = s.scan_until regexp_span_like if like s.pos -= 1 pre = s.pre_match.to_s @spans.each do |e| #begin match = s.scan Document.const_get("REGEXP_#{e.to_s.upcase}") #rescue NameError # next #end if match return pre + send("scan_#{e.to_s}", s) + scan_spans(StringScanner.new(s.rest)) end end s.pos = last_pos + 1 else break end end return s.string end