Class: Beardown::Document
- Inherits:
-
Object
- Object
- Beardown::Document
- Defined in:
- lib/beardown/blocks/p.rb,
lib/beardown/document.rb,
lib/beardown/spans/mark.rb,
lib/beardown/blocks/head.rb,
lib/beardown/blocks/asset.rb,
lib/beardown/blocks/quote.rb,
lib/beardown/spans/hashtag.rb,
lib/beardown/spans/linkurl.rb,
lib/beardown/spans/codespan.rb,
lib/beardown/spans/linkpost.rb,
lib/beardown/blocks/blankline.rb,
lib/beardown/blocks/codeblock.rb,
lib/beardown/blocks/list_todo.rb,
lib/beardown/blocks/list_ordered.rb,
lib/beardown/blocks/line_separator.rb,
lib/beardown/blocks/list_unordered.rb,
lib/beardown/spans/bold_italic_underline_strike.rb
Constant Summary collapse
- REGEXP_P =
/^(\s*)(.+)\n?/
- START_CHAR_MARK =
":"
- REGEXP_MARK =
/::([^\s].*[^\s])::/
- REGEXP_MARK_NOSPACE =
/::([^\s]*)::/
- REGEXP_HEAD =
/^(\#{1,6})\s+(.+)(\n)?/
- REGEXP_ASSET =
/^\[assets\/(.+)\]\n?/
- REGEXP_QUOTE =
/^>\s+(.+)\n?/
- START_CHAR_HASHTAG =
"#"
- REGEXP_HASHTAG_FULL =
/#([^\s^#][^#]*[^\s^#])#/
- REGEXP_HASHTAG_FULL_NOSPACE =
/#([^#^\s]+)#/
- REGEXP_HASHTAG =
/#([^#\s\,]+)/
- REGEXP_LINKURL =
START_CHAR_LINKURL = “[” # duplicate with LINKPOST
/\[(.+?)\]\((.+?)\)/
- START_CHAR_CODESPAN =
"`"
- REGEXP_CODESPAN =
/`(.*?)`/
- START_CHAR_LINKPOST =
"["
- REGEXP_LINKPOST =
/\[\[(.+)\]\]/
- REGEXP_BLANKLINE =
/^\s*?\n/
- REGEXP_CODEBLOCK =
/^```(.*)\n((.*\n)*?)^```\n?/
- REGEXP_LIST_TODO =
/^(\t*) *([\+\-])\s+(.+)\n?/
- REGEXP_LIST_ORDERED =
/^(\t*) *(\d+)\.\s+(.+)\n?/
- REGEXP_LINE_SEPARATOR =
/^---(\n)?/
- REGEXP_LIST_UNORDERED =
/^(\t*) *\*\s+(.+)(\n)?/
- START_CHAR_BOLD =
"*"
- START_CHAR_ITALIC =
"/"
- START_CHAR_UNDERLINE =
"_"
- START_CHAR_STRIKE =
"-"
- REGEXP_BOLD =
/\*([^\s].*?[^\s])\*/
- REGEXP_ITALIC =
/\/([^\s].*?[^\s])\//
- REGEXP_UNDERLINE =
/\_([^\s].*?[^\s])\_/
- REGEXP_STRIKE =
/\-([^\s].*?[^\s])\-/
Instance Method Summary collapse
- #add_extension_todo_svg ⇒ Object
- #check_color(tag) ⇒ Object
-
#content ⇒ Object
def assets return @assets unless defined?(@assets).nil? end.
- #convert_asset(filename) ⇒ Object
- #convert_blankline ⇒ Object
- #convert_codeblock(content, code) ⇒ Object
- #convert_codespan(code) ⇒ Object
- #convert_hashcolor(hex) ⇒ Object
- #convert_hashtag(tag) ⇒ Object
- #convert_head(id, content) ⇒ Object
- #convert_linkpost(title) ⇒ Object
- #convert_linkurl(title, url) ⇒ Object
- #convert_list_ordered(t_count, start_num, content) ⇒ Object
- #convert_list_todo(t_count, check_status, content) ⇒ Object
- #convert_list_unordered(t_count, content) ⇒ Object
- #convert_p(indent, content) ⇒ Object
- #convert_quote(content) ⇒ Object
- #html ⇒ Object
-
#initialize(text) ⇒ Document
constructor
A new instance of Document.
- #is_img?(filename) ⇒ Boolean
- #render_hashtag(tag) ⇒ Object
- #render_head(id, content) ⇒ Object
- #scan_asset(s) ⇒ Object
- #scan_blankline(s) ⇒ Object
- #scan_codeblock(s) ⇒ Object
- #scan_codespan(s) ⇒ Object
- #scan_hashtag(s) ⇒ Object
- #scan_head(s) ⇒ Object
- #scan_line_separator(s) ⇒ Object
-
#scan_linkpost(s) ⇒ Object
I use this not as a X callback url Scheme Instead, A url to this website used in Miyano Blog system.
- #scan_linkurl(s) ⇒ Object
- #scan_list_ordered(s) ⇒ Object
- #scan_list_todo(s) ⇒ Object
- #scan_list_unordered(s) ⇒ Object
- #scan_p(s) ⇒ Object
- #scan_quote(s) ⇒ Object
- #summary ⇒ Object
- #tags ⇒ Object
- #title ⇒ Object
- #to_html ⇒ Object
Constructor Details
#initialize(text) ⇒ Document
Returns a new instance of Document.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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 |
Instance Method Details
#add_extension_todo_svg ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# 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) ⇒ Object
37 38 39 40 |
# 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 ⇒ Object
def assets
return @assets unless defined?(@assets).nil?
end
34 35 36 37 |
# File 'lib/beardown/document.rb', line 34 def content return @content if defined? @content @content = @text.delete "\n", "\t", "\s", "#", "*", "`", "*", "-", "_", "=", "+", "[", "]", ":", "<", ">" end |
#convert_asset(filename) ⇒ Object
10 11 12 13 14 15 16 |
# 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 ⇒ Object
9 10 11 |
# File 'lib/beardown/blocks/blankline.rb', line 9 def convert_blankline "<br>\n" end |
#convert_codeblock(content, code) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# 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) ⇒ Object
11 12 13 |
# File 'lib/beardown/spans/codespan.rb', line 11 def convert_codespan(code) %(<code class="code-inline">#{code}</code>) end |
#convert_hashcolor(hex) ⇒ Object
42 43 44 |
# 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) ⇒ Object
31 32 33 34 35 |
# 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) ⇒ Object
16 17 18 |
# File 'lib/beardown/blocks/head.rb', line 16 def convert_head(id,content) "<h#{id}>#{content}</h#{id}>\n" end |
#convert_linkpost(title) ⇒ Object
14 15 16 |
# File 'lib/beardown/spans/linkpost.rb', line 14 def convert_linkpost(title) %(<a href="../#{title}/">#{title}</a>) end |
#convert_linkurl(title, url) ⇒ Object
11 12 13 |
# 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) ⇒ Object
11 12 13 |
# 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) ⇒ Object
19 20 21 22 23 |
# 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) ⇒ Object
10 11 12 |
# 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) ⇒ Object
11 12 13 |
# 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) ⇒ Object
10 11 12 |
# File 'lib/beardown/blocks/quote.rb', line 10 def convert_quote(content) "<blockquote>#{scan_spans(StringScanner.new(content))}</blockquote>\n" end |
#html ⇒ Object
45 46 47 48 |
# File 'lib/beardown/document.rb', line 45 def html return @html if defined? @html to_html end |
#is_img?(filename) ⇒ Boolean
18 19 20 21 22 23 24 25 26 |
# 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) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/beardown/spans/hashtag.rb', line 46 def render_hashtag(tag) tag = tag.downcase = tag.split "/" _tag = [0] @tags << _tag unless @tags.include? _tag [1..-1].each do |t| _tag += "/#{t}" @tags << _tag unless @tags.include? _tag end end |
#render_head(id, content) ⇒ Object
12 13 14 |
# File 'lib/beardown/blocks/head.rb', line 12 def render_head(id, content) @title << content if id==1&&@title.empty? end |
#scan_asset(s) ⇒ Object
5 6 7 8 |
# File 'lib/beardown/blocks/asset.rb', line 5 def scan_asset(s) filename = s[1] convert_asset filename end |
#scan_blankline(s) ⇒ Object
5 6 7 |
# File 'lib/beardown/blocks/blankline.rb', line 5 def scan_blankline(s) convert_blankline end |
#scan_codeblock(s) ⇒ Object
6 7 8 9 10 |
# 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) ⇒ Object
6 7 8 9 |
# File 'lib/beardown/spans/codespan.rb', line 6 def scan_codespan(s) code = s[1] convert_codespan code end |
#scan_hashtag(s) ⇒ Object
22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
5 6 7 8 9 10 |
# 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) ⇒ Object
5 6 7 |
# File 'lib/beardown/blocks/line_separator.rb', line 5 def scan_line_separator(s) "<hr>\n" end |
#scan_linkpost(s) ⇒ Object
I use this not as a X callback url Scheme Instead, A url to this website used in Miyano Blog system
9 10 11 12 |
# File 'lib/beardown/spans/linkpost.rb', line 9 def scan_linkpost(s) title = s[1] convert_linkpost(title) end |
#scan_linkurl(s) ⇒ Object
6 7 8 9 |
# 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) ⇒ Object
4 5 6 7 8 9 |
# 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) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# 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) ⇒ Object
4 5 6 7 8 |
# 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) ⇒ Object
5 6 7 8 9 |
# 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) ⇒ Object
5 6 7 8 |
# File 'lib/beardown/blocks/quote.rb', line 5 def scan_quote(s) content = s[1] convert_quote(content) end |
#summary ⇒ Object
39 40 41 42 43 |
# File 'lib/beardown/document.rb', line 39 def summary return @summary if defined? @summary s = content @summary = s[0...30] end |
#tags ⇒ Object
26 27 28 |
# File 'lib/beardown/document.rb', line 26 def @tags end |
#title ⇒ Object
22 23 24 |
# File 'lib/beardown/document.rb', line 22 def title @title end |
#to_html ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# 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 |