anyas-utilittys/mdtw.rb

117 lines
3.2 KiB
Ruby
Executable file

#!/usr/bin/env ruby
FMT = {
:re => "\x11[s7000]", # reset
:h1 => "\x11[sD050]", # [x] # heading 1
:h2 => "\x11[s5040]", # [x] ## heading 2
:h3 => "\x11[s4030]", # [x] ### heading 3
:h4 => "\x11[s4020]", # [x] #### heading 4
:em => "\x11[s7010]", # [x] **bold** __bold__
:it => "\x11[s7020]", # [x] *italic* _italic_
:bi => "\x11[s7030]", # [x] ___bold italic___ ***...***
:hi_it => "\x11[sA020]", # [x] _=highlighted italic=_ *=...=*
:hi => "\x11[sA000]", # [x] ==highlight==
:co => "\x11[sE800]", # [x] `code` ``...``
:un => "\x11[s2040]", # [x] ~~underline~~
:x => "\x11[sC000]", # [x] * list bullet
}
def dohelp
puts "MDTW - Markdown Trans Woman"
puts "Pass markdown files to MDTW and recieve output"
puts " of woman pages with itty escapes."
end
def dorun(args)
files = []
args.each do |arg|
if arg == '--help'
dohelp;exit
elsif arg == '—help'
dohelp;exit
elsif arg == '-h'
dohelp;exit
end
files << arg
end
files.each do |filename|
if filename == '-'
puts convert(STDIN.read)
next
end
File.open(filename, 'r') do |f|
puts convert(f.read)
end
end
end
def convert(file_input)
file_input.gsub!( /^\#{1} (?<text>.*)$/ ) do |match|
next "#{FMT[:h1]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /^\#{2} (?<text>.*)$/ ) do |match|
next "#{FMT[:h2]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /^\#{3} (?<text>.*)$/ ) do |match|
next "#{FMT[:h3]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /^\#{4} (?<text>.*)$/ ) do |match|
next "#{FMT[:h4]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /^(?<indent>[ \t]*)(?<aro>\>+)(?=[ \t].*$)/ ) do |match|
denter = "\t" * $~[:aro].length
next "#{$~[:indent]}#{denter}"
end
file_input.gsub!( /^(?<indent>[ \t]*)(?<x>([\-\*]|(?<num>[0-9]+)[\.\)]))(?= .*$)/ ) do |match|
ilevel = $~[:indent].each_char.reduce(0) do |sum,ch|
case ch
when ' ' then sum += 1
when '\t' then sum += 4
else sum
end
end
ilevel = ilevel / 4
bullet = $~[:x]
if bullet == '-' || bullet == '*'
bullet = ["\u2022", "\u25E6", "\u25AA"][ilevel % 3]
elsif $~[:num]
# do nothing, case here for conveinence
end
next "#{$~[:indent]}#{FMT[:x]}#{bullet}#{FMT[:re]}"
end
file_input.gsub!( /^(?<it>[_\-\*])\k<it>{2}$/ ) do |match|
next $~[:it] * 20
end
file_input.gsub!( /~~(?<text>.+?)~~/ ) do |match|
next "#{FMT[:un]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /==(?<text>.+?)==/ ) do |match|
next "#{FMT[:hi]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /(?<it>[_\*])=(?<text>.+?)=\k<it>/ ) do |match|
next "#{FMT[:hi_it]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /(?<it>[_\*])(?<text>[^_\*]+?)\k<it>/ ) do |match|
next "#{FMT[:it]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /(?<it>[_\*])\k<it>(?<text>[^_\*]+?)\k<it>{2}/ ) do |match|
next "#{FMT[:em]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /(?<it>[_\*])\k<it>{2}(?<text>[^_\*]+?)\k<it>{3}/ ) do |match|
next "#{FMT[:bi]}#{$~[:text]}#{FMT[:re]}"
end
file_input.gsub!( /(?<it>``?)(?<text>[^`]+?)\k<it>/ ) do |match|
next "#{FMT[:co]}#{$~[:text]}#{FMT[:re]}"
end
end
dorun ARGV