Created
March 23, 2016 00:37
-
-
Save pstaender/af8d4e157452439361d7 to your computer and use it in GitHub Desktop.
Convert basic latex to plaintext
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env coffee | |
process.stdin.on 'data', (s) -> | |
line = s.toString().replace(/\n/g, '_newline_') | |
replaceRuleset = [ | |
# remove tables | |
[ /\\begin\{(table)\}.*?\\end\{(table)\}/ig, '' ] | |
# remove tables | |
[ /\\begin\{(tabular)\}.*?\\end\{(tabular)\}/ig, '' ] | |
# remove figures | |
[ /\\begin\{(figure)\}.*?\\end\{(figure)\}/ig, '' ] | |
# remove text formatter | |
[ /\\(text|textit|textbf)(\[.+\])*\{(.*?)\}/ig, '$3' ] | |
# transform headin | |
[ /\\(section|subsection|subsubsection)(\[.+\])*\{(.*?)\}/ig, '## $3' ] | |
# transform annotations | |
[ /\\(cite|ref|footnote|footnotetext|text|textit|textbf|url)(\[.+\])*\{(.*?)\}/ig, '[$3]' ] | |
# remove comments, now workimg, yet?! | |
[ /_newline_\s*%.+?_newline_/ig, '_newline_' ] | |
# clean lists | |
[ /\\(begin|end)\{(enumerate|itemize)\}/ig, '' ] | |
# transform lists | |
[ /(\s+)\\item\s+/ig, ' * '] | |
# unescape | |
[ /\\(.){1}/ig, '$1' ] | |
] | |
replaceRuleset.forEach (rule) -> | |
line = line.replace(rule[0], rule[1]) | |
line = line.replace(/_newline_/g, "\n") | |
console.log line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment