Prototype

This commit is contained in:
Yessiest 2023-02-27 08:45:00 +04:00
parent 60821677af
commit a022377f08
2 changed files with 69 additions and 15 deletions

View File

@ -79,7 +79,7 @@ module Markdown
x.gsub(/^(?<!\\)(\#{1,4})([^\n\r]*)/) { x.gsub(/^(?<!\\)(\#{1,4})([^\n\r]*)/) {
level,content = Regexp.last_match[1..2] level,content = Regexp.last_match[1..2]
"<h#{level.length}>"+content+"</h#{level.length}>" "<h#{level.length}>"+content+"</h#{level.length}>"
} }.gsub(/^\-{3,}/,"<hr>")
end.join("\n") end.join("\n")
super super
end end
@ -93,10 +93,10 @@ module Markdown
super() super()
end end
def to_html def to_html
@output = @input.gsub(/(?<=\n)(?<!\\)```([\w_-]*)(.*?)```/) { @output = @input.gsub(/(?:\n|^)```([\w_-]*)([\s\S]+?)```/) {
language,code = Regexp.last_match[1..2] language,code = Regexp.last_match[1..2]
code = Markdown::html_highlighter.call(language,code) code = Markdown::html_highlighter.call(language,code) if Markdown::html_highlighter
"<code>#{code}</code>" "<pre><code>#{code.gsub /[|#*`~_!\[]/,"\\\\\\0"}</code></pre>"
} }
super() super()
end end
@ -162,28 +162,56 @@ module Markdown
tables = [] tables = []
cur_table = [] cur_table = []
lines.each_with_index { |line,index| lines.each_with_index { |line,index|
if (line.start_with? / *\|/) and (line.match /^ *\|.*\|/) then if (table_start != -1) and (line.match /^\s*\|([^\|]*\|){#{table_column_count-1}}$/) 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_testline == -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 table_testline = 1
else else
table_start = -1 table_start = -1
cur_table = [] cur_table = []
end end
else else
cur_table.push (line.split "|") cur_table.push (line.split("|").filter_map { |x| x.strip if x.match /\S+/ })
end 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 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? "#") ? " <th>"+x.sub(/^#\s+/,"")+"</th>" : " <td>"+x+"</td>"}).prepend(" <tr>").append(" </tr>")
end).flatten.prepend("<table>").append("</table>")
}
@output = lines.join("\n")
super() super()
end end
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 end

30
test.rb
View File

@ -62,12 +62,31 @@ piss__
CODE CODE
).to_html ).to_html
puts (Markdown::QuoteTranslator.new(<<TEXT test = (Markdown::CodeBlockTranslator.new(<<TEXT
# Markdown garbage gallery
## Header level 2
### Header level 3
#### Header level 4
__[Underlined Link](https://google.com)__
__**unreal shitworks**__
split
---
![Fucking image idk](https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Ftse3.explicit.bing.net%2Fth%3Fid%3DOIP.qX1HmpFNHyaTfXv-SLnAJgHaDD%26pid%3DApi&f=1&ipt=dc0e92fdd701395eda76714338060dcf91c7ff9e228f108d8af6e1ba3decd1c2&ipo=images)
> Here's a bunch of shit i guess lmao idk > Here's a bunch of shit i guess lmao idk
```markdown ```markdown
test test
test test
test test
|1|2|3|
|-|-|-|
|a|b|c|
| uneven rows | test | yes |
|-|-|-|
| sosiska | dinozavri | suda pihaem |
| sosiska 2 | vitalya 2 | brat 2 |
*** test *** *** test ***
piss piss
cock cock
@ -87,6 +106,13 @@ __cock__
> __cum__ > __cum__
__mashup__ __mashup__
| # sosiska | sosiska | suda pihaem |
|-|-|-|
| # 2 | chuvak ya ukral tvayu sardelku ))0)))0))))))) | __blya ((9((9((9)__ |
| # azazaz lalka sasI | test | test |
TEXT TEXT
)+Markdown::QuoteTranslator+Markdown::LeftmostTagTranslator+Markdown::LinearTagTranslator) )+Markdown::QuoteTranslator+Markdown::LeftmostTagTranslator+Markdown::LinearTagTranslator+Markdown::TableTranslator+Markdown::BackslashTranslator)
.to_html .to_html
write = File.new("/tmp/test.html","w")
write.write(test)
write.close