Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A crude script to convert Textile documents to AsciiDoc. Some of the replacements are specific to markup that I use, but hopefully still useful.
#!/usr/bin/env ruby
if ARGV[0].nil?
$stderr.puts 'Must specify an input file'
exit 1
end
source = IO.read(ARGV[0])
title = nil
author = nil
# map Awestruct and Jekyll front matter to AsciiDoc attribute entries
source.gsub!(/^---\n(.*?)\n^---/m) {
fm = $1
fm.gsub!(/^(\w+): (.*)/, ':\1: \2')
fm.gsub!(/:title: "([^"]+)"\n/) { title = $1; '' }
fm.gsub!(/:author: (.*)\n/) { author = $1; '' }
"#{fm}\n"
}
# p(lead). text
source.gsub!(/^p\((.*?)\)\. /, "[role=\\1]\n")
# bq. text or p((. text
source.gsub!(/^(?:p\(\(|bq)\. (.*)/, "____\n\\1\n____")
# h2. text
source.gsub!(/^h2\. /, '== ')
# h3. text
source.gsub!(/^h3\. /, '=== ')
# h4{margin-top: -1em}. text (specific to my documents)
source.gsub!(/^h4{margin-top: -1em}\. /, "[discrete, role=byline]\n==== ")
# # text (numbered bullets)
source.gsub!(/^# /, '. ')
# - label := def
source.gsub!(/^- (.*?) := (.*)/, '\1:: \2')
# *NOTE*: text
source.gsub!(/^\*(NOTE|TIP):\* /, '\1: ')
# "target":uri
source.gsub!(/\[?"([^"]+)":(https?:\/\/[^\s,\)\]]+)\]?/) {
suffix = ''
text = $1
url = $2
if url.end_with?('.') || url.end_with?('!')
suffix = "#{suffix}#{url[-1]}"
url.chop!
end
"#{url}[#{text}]#{suffix}"
}
# @text@
source.gsub!(/@([^@]+)@/, '+\1+')
# !/images/image.png(alt text)! (alt text optional)
# p=. !/images/image.png(alt text)! (alt text optional)
source.gsub!(/(p=\. )?!(?:\((.*?)\))?\/images\/([^\(]+?)(?:\(([^\)]+)\))?!/) {
block = $~.pre_match.end_with?("\n") && $~.post_match.start_with?("\n")
pos = $1
role = $2
uri = $3
text = $4
buf = "image#{block ? '::' : ':'}#{uri}["
if text
buf = "#{buf}#{text}"
end
if !role && pos && pos.end_with?('.')
if text
buf = "#{buf}, "
end
buf = "#{buf}role=ctr"
end
if role
if text
buf = "#{buf}, "
end
buf = "#{buf}role=#{role}"
end
buf = "#{buf}]"
buf
}
# bc. code
source.gsub!(/bc\. (.*?)^$/m, "....\n\\1....\n")
# p. text
source.gsub!(/p\. (.*)/, '\1')
# "text"
source.gsub!(/"([^"]+)"/, "``\\1''")
# <sup>text</sup>
source.gsub!(/<sup>(.*?)<\/sup>/, '^\1^')
# reverse order of _ and * around text
source.gsub!(/_\*([^\*]+)\*_/, '*_\1_*')
# output document
puts "= #{title}\n"
puts "#{author}\n"
puts source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.