diff --git a/markdown.rb b/markdown.rb index 3be5015..cb3d380 100644 --- a/markdown.rb +++ b/markdown.rb @@ -79,7 +79,7 @@ module Markdown x.gsub(/^(?"+content+"" - } + }.gsub(/^\-{3,}/,"
") end.join("\n") super end @@ -93,10 +93,10 @@ module Markdown super() end def to_html - @output = @input.gsub(/(?<=\n)(?#{code}" + code = Markdown::html_highlighter.call(language,code) if Markdown::html_highlighter + "
#{code.gsub /[|#*`~_!\[]/,"\\\\\\0"}
" } super() end @@ -162,28 +162,56 @@ module Markdown tables = [] cur_table = [] lines.each_with_index { |line,index| - if (line.start_with? / *\|/) and (line.match /^ *\|.*\|/) then - table_start = index - table_column_count = line.count "|" - cur_table.push (line.split "|") - end - if (table_start != -1) and (line.match /^ *\|([^\|]*\|){#{table_column_count-1}}$/) then + if (table_start != -1) and (line.match /^\s*\|([^\|]*\|){#{table_column_count-1}}$/) then if (table_testline == -1) then - if (line.match /^ *\|(\-*\|){#{table_column_count-1}}$/) then + if (line.match /^\s*\|(\-*\|){#{table_column_count-1}}$/) then table_testline = 1 else table_start = -1 cur_table = [] end else - cur_table.push (line.split "|") + cur_table.push (line.split("|").filter_map { |x| x.strip if x.match /\S+/ }) end + elsif (table_start != -1) then + obj = {table: cur_table, start: table_start, end: index} + tables.push(obj) + table_start = -1 + cur_table = [] + table_testline = -1 + table_column_count = 0 + end + if (table_start == -1) and (line.start_with? /\s*\|/ ) and (line.match /^\s*\|.*\|/) then + table_start = index + table_column_count = line.count "|" + cur_table.push (line.split("|").filter_map { |x| x.strip if x.match /\S+/ }) end - puts cur_table } + if cur_table != [] then + obj = {table: cur_table, start:table_start, end: lines.count-1} + tables.push(obj) + end + tables.reverse.each { |x| + lines[x[:start]..x[:end]] = (x[:table].map do |a2d| + (a2d.map { |x| (x.start_with? "#") ? " "+x.sub(/^#\s+/,"")+"" : " "+x+""}).prepend(" ").append(" ") + end).flatten.prepend("").append("
") + } + @output = lines.join("\n") super() end end + + # Backslash cleaner + # Cleans excessive backslashes after the translation + class BackslashTranslator < AbstractTranslator + def initialize(text) + @input = text + @output = text + end + def to_html + @output = @input.gsub(/\\(.)/,"\\1") + end + end end diff --git a/test.rb b/test.rb index d7cd45e..48cbeb5 100644 --- a/test.rb +++ b/test.rb @@ -62,12 +62,31 @@ piss__ CODE ).to_html -puts (Markdown::QuoteTranslator.new(< Here's a bunch of shit i guess lmao idk ```markdown test test test +|1|2|3| +|-|-|-| +|a|b|c| + +| uneven rows | test | yes | +|-|-|-| +| sosiska | dinozavri | suda pihaem | +| sosiska 2 | vitalya 2 | brat 2 | *** test *** piss cock @@ -87,6 +106,13 @@ __cock__ > __cum__ __mashup__ +| # sosiska | sosiska | suda pihaem | +|-|-|-| +| # 2 | chuvak ya ukral tvayu sardelku ))0)))0))))))) | __blya ((9((9((9)__ | +| # azazaz lalka sasI | test | test | TEXT - )+Markdown::QuoteTranslator+Markdown::LeftmostTagTranslator+Markdown::LinearTagTranslator) + )+Markdown::QuoteTranslator+Markdown::LeftmostTagTranslator+Markdown::LinearTagTranslator+Markdown::TableTranslator+Markdown::BackslashTranslator) .to_html +write = File.new("/tmp/test.html","w") +write.write(test) +write.close