💾 Archived View for gemini.ctrl-c.club › ~de_alchmst › data › to-gemtext.rb.txt captured on 2024-05-10 at 14:48:53.

View Raw

More Information

➡️ Next capture (2024-05-12)

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

#!/usr/bin/ruby

f = File.open(ARGV[0], "r")
# separate by blocks so that I can leave them unformated
block_separated = f.read.sub(/;;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
    # handle newline stuff
    while part.gsub! /^([^#].+)\n([^\n#\*])/, '\1 \2'
    end

    # quoted text
    part.gsub! /\n?[^\n\S]*`(.+?)`[^\n\S]*\n?/, "\n" + '> \1' + "\n"

    # links
    while part.sub! /\n?[^\n\S]*\[(.+?)\]\((.+?)\)[^\n\S]*\n?/, \
                    "\n=> "+'\2 \1'+"\n"
    end

    # unused inline formatting
    part.gsub! /[_\*\/]{2}(.+?)[_\*\/]{2}/, '\1'

    outcome += part
  else
    # inside blocks
    outcome += "```#{part}```"

  end
end

# return
print outcome.strip