Skip to content

Instantly share code, notes, and snippets.

@citrus-lemon
Last active October 5, 2016 15:09
Show Gist options
  • Save citrus-lemon/be7033a32d13cbd20c8dfa089903cba2 to your computer and use it in GitHub Desktop.
Save citrus-lemon/be7033a32d13cbd20c8dfa089903cba2 to your computer and use it in GitHub Desktop.
Input TeX textfile or zip archive and output pdf file
#!/usr/bin/env ruby
$files = STDIN.read
randir = (0...31).map { (65 + rand(26)).chr }.join
class String
def black; "\e[30m#{self}\e[0m" end
def red; "\e[31m#{self}\e[0m" end
def green; "\e[32m#{self}\e[0m" end
def brown; "\e[33m#{self}\e[0m" end
def blue; "\e[34m#{self}\e[0m" end
def magenta; "\e[35m#{self}\e[0m" end
def cyan; "\e[36m#{self}\e[0m" end
def gray; "\e[37m#{self}\e[0m" end
def bg_black; "\e[40m#{self}\e[0m" end
def bg_red; "\e[41m#{self}\e[0m" end
def bg_green; "\e[42m#{self}\e[0m" end
def bg_brown; "\e[43m#{self}\e[0m" end
def bg_blue; "\e[44m#{self}\e[0m" end
def bg_magenta; "\e[45m#{self}\e[0m" end
def bg_cyan; "\e[46m#{self}\e[0m" end
def bg_gray; "\e[47m#{self}\e[0m" end
def bold; "\e[1m#{self}\e[22m" end
def italic; "\e[3m#{self}\e[23m" end
def underline; "\e[4m#{self}\e[24m" end
def blink; "\e[5m#{self}\e[25m" end
def reverse_color; "\e[7m#{self}\e[27m" end
end
# temp directory
$DIR = "/tmp/texify_#{randir}"
`mkdir #{$DIR}`
IO.popen(["file", "-"], "r+") do |pipe|
pipe.write $files
pipe.close_write
$type = pipe.read
end
def tex
File.open("#{$DIR}/main.tex", "w") do |file|
file.write($files)
end
IO.popen([
"pdflatex",
"-aux-directory=#{$DIR}",
"-output-directory=#{$DIR}",
"#{$DIR}/main.tex"]) do |pipe|
STDERR.puts "pdf LaTeX log file".cyan
STDERR.puts pipe.read
end
$OUT = "#{$DIR}/main.pdf"
end
def zip
File.open("#{$DIR}/archive.zip", "w") do |file|
file.write($files)
end
`mkdir #{$DIR}/archive`
IO.popen(["unzip", "#{$DIR}/archive.zip", "-d", "#{$DIR}/archive"]) do |pipe|
STDERR.puts "unzip log file".cyan
STDERR.puts pipe.read
end
x = ""
d = ""
Dir.foreach("#{$DIR}/archive") do |entry|
if entry =~ /\.tex$/
x = entry
end
if File::directory?("#{$DIR}/archive/#{entry}")
if (entry =~ /^[_\.]/) == nil
d = entry
end
end
end
if x != ""
IO.popen([
"pdflatex",
"-aux-directory=#{$DIR}/archive",
"-output-directory=#{$DIR}/archive",
"#{$DIR}/archive/#{x}"]) do |pipe|
STDERR.puts "pdf LaTeX log file".cyan
STDERR.puts pipe.read
end
$OUT = "#{$DIR}/archive/#{x.gsub(/\.tex$/, '.pdf')}"
elsif d != ""
Dir.foreach("#{$DIR}/archive/#{d}") do |entry|
if entry =~ /\.tex$/
x = entry
end
end
if x != ""
IO.popen([
"pdflatex",
"-aux-directory=#{$DIR}/archive/#{d}",
"-output-directory=#{$DIR}/archive/#{d}",
"#{$DIR}/archive/#{d}/#{x}"]) do |pipe|
STDERR.puts "pdf LaTeX log file".cyan
STDERR.puts pipe.read
end
$OUT = "#{$DIR}/archive/#{d}/#{x.gsub(/\.tex$/, '.pdf')}"
else
STDERR.puts "ERR no tex file".bg_red
end
else
STDERR.puts "ERR no tex file".bg_red
end
end
if $type =~ /Zip archive data/
STDERR.puts 'ZIP mode'.green
zip
else
# tex source file
STDERR.puts 'TEX mode'.green
tex
end
File.open($OUT, "r") do |file|
puts file.read
end
# clear temp files
`rm -rf #{$DIR}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment