💾 Archived View for gemini.ctrl-c.club › ~de_alchmst › data › to-gemtext.rb.txt captured on 2024-05-12 at 16:11:59.

View Raw

More Information

⬅️ Previous capture (2024-05-10)

➡️ Next capture (2024-07-08)

🚧 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.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 / images
    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