💾 Archived View for gemini.ctrl-c.club › ~de_alchmst › data › to-html.rb.txt captured on 2024-05-26 at 16:34:50.

View Raw

More Information

⬅️ Previous capture (2024-05-12)

🚧 View Differences

-=-=-=-=-=-=-

#!/usr/bin/ruby

f = File.open(ARGV[0], "r")
# separate by blocks so that I can leave them unformated
block_separated = f.read.gsub("&", "&amp;").gsub("<","&lt;").gsub(">", "&gt;") \
                  .gsub(/;;TABLE(;;.+;;)/, \
                        "```\n"+';;ASCII_TABLE\1'+"\n```").split /^```/
f.close

outcome = ""

# go through file
for i in 0..block_separated.length-1 do
  part = block_separated[i]

  # format if not in block
  if i % 2 == 0
    # headers
    part.gsub! /^### (.+)/, '<h3>\1</h3>'
    part.gsub! /^## (.+)/, '<h2>\1</h2>'
    part.gsub! /^# (.+)/, '<h1>\1</h1>'

    # bold
    part.gsub! /\*\*(.+?)\*\*/, '<b>\1</b>'
    # italic
    part.gsub! /\/\/(.+?)\/\//, '<i>\1</i>'
    # underscore
    part.gsub! /__(.+?)__/, '<u>\1</u>'
    # quote
    part.gsub! /`(.+?)`/, '<q>\1</q>'

    # link
    part.gsub! /\[(.+?)\]\((.+?)\)/, '<a href="\2">\1</a>'

    # img
    part.gsub! /\{(.+?)\}\((.+?)\)/, '<img src="\2">'

    # lists
    part.gsub! /^\* (.+)/, '<li>\1</li>'
    part.gsub! /(<\/li>\n)(?!<li>)/, '\1</ul>' + "\n"
    part.gsub! /^(?<!<\/li>)(\n<li>)/, "\n" + '<ul>\1'

    # paragrafs
    part.gsub! /\n\n(?!\n|<ul|<h)/, "\n\n<p>\n"
    part.gsub! /(?<!\n|ul>|h\d>)\n\n/, "\n</p>\n\n"

    outcome += part
  else
    # inside blocks
    outcome += "<pre>#{part}</pre>"

  end
end

print outcome.strip