module MaRuKu::In::Markdown::SpanLevelParser include MaRuKu::Helpers EscapedCharInText = '\\`*_{}[]()#.!|:+->'.split(//) EscapedCharInQuotes = EscapedCharInText + ["'", '"'] EscapedCharInInlineCode = ['\\', '`'] IgnoreWikiLinks = MaRuKu::Globals[:ignore_wikilinks] def parse_span(string, parent=nil) string = Array(string).join("\n") unless string.kind_of? String src = MaRuKu::In::Markdown::SpanLevelParser::CharSource.new(string, parent) read_span(src, EscapedCharInText, [nil]) end # This is the main loop for reading span elements # # It's long, but not *complex* or difficult to understand. # # def read_span(src, escaped, exit_on_chars=nil, exit_on_strings=nil) escaped = Array(escaped) con = SpanContext.new c = d = nil while true c = src.cur_char # This is only an optimization which cuts 50% of the time used. # (but you can't use a-zA-z in exit_on_chars) if c && c =~ /a-zA-Z0-9/ con.push_char src.shift_char next end break if Array(exit_on_chars).include?(c) if Array(exit_on_strings).any? {|x| src.cur_chars_are x } # Special case: bold nested in italic break unless !(['*', '_'] & Array(exit_on_strings)).empty? && ['**', '__'].include?(src.cur_chars(2)) && !['***', '___'].include?(src.cur_chars(3)) end # check if there are extensions next if check_span_extensions(src, con) case c = src.cur_char when ' ' if src.cur_chars_are " \n" src.ignore_chars(3) con.push_element md_br next else src.ignore_char con.push_space end when "\n", "\t" src.ignore_char con.push_space when '`' read_inline_code(src, con) when '<' # It could be: # 1) HTML "