class TDiary::Style::MarkdownSection
Public Class Methods
new(fragment, author = nil)
click to toggle source
# File lib/tdiary/style/markdown.rb, line 8 def initialize(fragment, author = nil) @author = author @subtitle, @body = fragment.split(/\n/, 2) @subtitle.sub!(/^\#\s*/,'') @body ||= '' @categories = get_categories @stripped_subtitle = strip_subtitle @subtitle_to_html = @subtitle ? to_html('# ' + @subtitle).gsub(/\A<h\d>|<\/h\d>\z/io, '') : nil @stripped_subtitle_to_html = @stripped_subtitle ? to_html('# ' + @stripped_subtitle).gsub(/\A<h\d>|<\/h\d>\z/io, '') : nil @body_to_html = to_html(@body) end
Public Instance Methods
categories=(categories)
click to toggle source
# File lib/tdiary/style/markdown.rb, line 27 def categories=(categories) @subtitle = "#{categories_to_string} " + (strip_subtitle || '') @strip_subtitle = strip_subtitle end
do_html4(date, idx, opt)
click to toggle source
# File lib/tdiary/style/markdown.rb, line 38 def do_html4(date, idx, opt) subtitle = to_html('# ' + @subtitle) subtitle.sub!( %r!<h3>(.+?)</h3>!m ) do "<h3><%= subtitle_proc( Time.at( #{date.to_i} ), #{$1.dump.gsub( /%/, '\\\\045' )} ) %></h3>" end if opt['multi_user'] and @author then subtitle.sub!(/<\/h3>/,%Q|[#{@author}]</h3>|) end r = subtitle r << @body_to_html end
subtitle=(subtitle)
click to toggle source
# File lib/tdiary/style/markdown.rb, line 22 def subtitle=(subtitle) @subtitle = (subtitle || '').sub(/^# /,"\##{categories_to_string} ") @strip_subtitle = strip_subtitle end
to_src()
click to toggle source
# File lib/tdiary/style/markdown.rb, line 32 def to_src r = '' r << "\# #{@subtitle}\n" if @subtitle r << @body end
Private Instance Methods
get_categories()
click to toggle source
# File lib/tdiary/style/markdown.rb, line 133 def get_categories return [] unless @subtitle cat = /(\\?\[([^\[]+?)\\?\])+/.match(@subtitle).to_a[0] return [] unless cat cat.scan(/\\?\[(.*?)\\?\]/).collect do |c| c[0].split(/,/) end.flatten end
strip_subtitle()
click to toggle source
# File lib/tdiary/style/markdown.rb, line 142 def strip_subtitle return nil unless @subtitle r = @subtitle.sub(/^((\\?\[[^\[]+?\]\\?)+\s+)?/, '') if r.empty? nil else r end end
to_html(string)
click to toggle source
# File lib/tdiary/style/markdown.rb, line 52 def to_html(string) r = string.dup # 1. Stash plugin calls plugin_stashes = [] r.gsub!(/\{\{(.*?)\}\}/) do |matched| # Convert `{{ }}' to erb tags plugin_stashes.push([matched, "<%=#{$1}%>"]) "@@tdiary-style-markdown-plugin#{plugin_stashes.length - 1}@@" end # 2. Apply markdown conversion extensions = [:autolink, :table] options = [:HARDBREAKS, :UNSAFE] renderer = HTMLwithRouge.new(options: options, extensions: extensions) doc = CommonMarker.render_doc(r, :DEFAULT, extensions) r = renderer.render(doc) # 3. Stash <pre> and <code> tags pre_tag_stashes = [] r.gsub!(/<pre(.*?)<\/pre>/m) do |matched| pre_tag_stashes.push(matched) "@@tdiary-style-markdown-pre_tag#{pre_tag_stashes.length - 1}@@" end code_tag_stashes = [] r.gsub!(/<code(.*?)<\/code>/m) do |matched| code_tag_stashes.push(matched) "@@tdiary-style-markdown-code_tag#{code_tag_stashes.length - 1}@@" end # 4. Convert miscellaneous r.gsub!(/>(.+?)</) do if $1.include?("<a ") ">#{$1}<" else ">#{Twitter::TwitterText::Autolink.auto_link_usernames_or_lists($1)}<" end end r = r.emojify # diary anchor r.gsub!(/<h(\d)/) { "<h#{$1.to_i + 2}" } r.gsub!(/<\/h(\d)/) { "</h#{$1.to_i + 2}" } # my syntax r.gsub!(/<a href="(\d{4}|\d{6}|\d{8}|\d{8}-\d+)[^\d]*?#?([pct]\d+)?">(.*?)<\/a>/) { unless $3.empty? %Q|<%=my "#{$1}#{$2}", "#{$3}" %>| else %Q|<%=my "#{$1}#{$2}", "#{$1}#{$2}" %>| end } # 5. Unstash <pre>, <code> and plugin call pre_tag_stashes.each.with_index do |str, i| plugin_stashes.each.with_index do |(p_str, p_erb), j| if str["@@tdiary-style-markdown-plugin#{j}@@"] str["@@tdiary-style-markdown-plugin#{j}@@"] = CGI.escapeHTML(p_str) end end r["@@tdiary-style-markdown-pre_tag#{i}@@"] = str end code_tag_stashes.each.with_index do |str, i| plugin_stashes.each.with_index do |(p_str, p_erb), j| if str["@@tdiary-style-markdown-plugin#{j}@@"] str["@@tdiary-style-markdown-plugin#{j}@@"] = CGI.escapeHTML(p_str) end end r["@@tdiary-style-markdown-code_tag#{i}@@"] = str end plugin_stashes.each.with_index do |(str, erb), i| if r["@@tdiary-style-markdown-plugin#{i}@@"] r["@@tdiary-style-markdown-plugin#{i}@@"] = erb end end r end