krambook/Rakefile

295 lines
9 KiB
Ruby
Raw Normal View History

2010-11-03 23:24:39 +00:00
#!/usr/bin/env ruby
# encoding: utf-8
require 'rake/clean'
CLEAN.include('**/*.{aux,log,out}')
CLEAN.include('tmp/**/*')
2010-11-03 23:24:39 +00:00
CLOBBER.include('**/*.pdf')
CLOBBER.include('content/**/*.tex')
2010-11-18 01:13:58 +00:00
CLOBBER.include('site/**/*.html')
2010-11-18 09:24:37 +00:00
CLOBBER.include('site/**/*.svg')
2010-11-18 01:13:58 +00:00
CLOBBER.include('site/include/*')
2010-11-18 09:24:37 +00:00
CLOBBER.exclude('site/js')
2010-11-03 23:24:39 +00:00
task :default => [:compile]
2010-11-18 15:14:42 +00:00
task :svg do
class HTMLCompile
def initialize
eval File.read('config.rb')
puts @pdfname
end
def run
FileUtils.mkdir_p("site")
if not FileTest.exists?( "tmp/#{@pdfname}.pdf" )
puts "run `rake compile` to generate the pdf file please"
exit 1
end
command=%{pdf2svg tmp/#{@pdfname}.pdf site/#{@pdfname}-%d.svg all}
system(command)
hdecal=110
vdecal=120
nb_pages=0
Dir["site/*.svg"].each do |fic|
f=File.open(fic,"r")
res=f.read().sub( /viewBox="(\d+) (\d+) (\d+) (\d+)"/) do
res=%{viewBox="}
res<<=%{#{Integer($1) + hdecal} }
res<<=%{#{Integer($2) + vdecal} }
res<<=%{#{Integer($3) - hdecal} }
res<<=%{#{Integer($4) - vdecal}"}
end
f.close
f=File.open(fic,"w")
f.write( res )
f.close
nb_pages+=1
end
target=File.open("site/index.html","w")
src=File.open("include/index.html","r")
res=src.read()
src.close
res.sub!("var nb_pages=0","var nb_pages=#{nb_pages}")
target.write(res)
target.close
end
end
x=HTMLCompile.new
x.run
end
2010-11-18 15:14:42 +00:00
task :html do
require 'rubygems'
require 'kramdown'
require 'filters/markdown_macros'
require 'filters/mkd_post_latex_macros_to_html'
2010-11-18 01:13:58 +00:00
require 'filters/html_template'
require 'filters/mathjax'
2010-11-18 18:00:25 +00:00
require 'filters/links'
class KrambookCompile
require 'config_html.rb'
attr_accessor :filelist
# take a string from kramdown
# returns LaTeX after filter
def compile_text(tmp)
@prefilters.each do |f|
tmp=f.run( tmp )
end
# compile to latex
tmp=Kramdown::Document.new(tmp, :latex_headers => %w(chapter section subsection paragraph subparagraph subsubparagraph)).to_html
# post filters
@postfilters.each{ |f| tmp=f.run(tmp) }
return tmp
end
def process_template
puts "PROCESS: "+@template_file
txt=File.read(@template_file)
# puts "READ: " + txt
txt.sub!( /<!-- INCLUDES -->/ ) do
puts "HERE"
@filelist.map do |source,dest|
2010-11-18 18:00:25 +00:00
%{<div class="block">
<h3>
2010-11-18 01:13:58 +00:00
<a href="#{dest.sub(/^site\//,'')}">
2010-11-18 08:45:18 +00:00
#{File::basename(dest,'.html').sub(/^\d+_/,'')}
<span class="nicer">»</span>
</a>
</h3>
</div>}
end.join("\n") + '</ul>'
end
# puts "AFTER INCLUDES: " + txt
txt.gsub!(%r{<!-- Author -->},@author)
# puts "AFTER AUTHOR: " + txt
txt.gsub!(%r{<!-- Title -->},@title)
txt.gsub!(%r{<!-- Subtitle -->},@subtitle)
# puts "AFTER TITLE: " + txt
txt.sub!( %r{<!-- HTML HEADER -->},@html_headers)
# puts "AFTER HTML HEADER: " + txt
2010-11-18 01:13:58 +00:00
fic=File.new("site/index.html","w")
fic.write(txt)
fic.close
end
def initialize
eval File.new('config_html.rb','r').read
@prefilters=[]
@prefilters<<=MarkdownMacros.new
@postfilters=[]
@postfilters<<=MarkdownPostLatexMacrosToHTML.new
2010-11-18 01:13:58 +00:00
html_template=HTMLTemplate.new
html_template.template=@general_template
html_template.title=@title
html_template.subtitle=@subtitle
html_template.author=@author
html_template.html_headers=@html_headers
html_template.homeURL="index.html"
2010-11-18 18:00:25 +00:00
@postfilters<<=Links.new
2010-11-18 01:13:58 +00:00
@postfilters<<=html_template
@postfilters<<=MathJax.new
@filelist=Dir.glob("content/**/*.md").sort.map do |fic|
2010-11-18 01:13:58 +00:00
[ fic, fic.sub(/^content\//,"site/").sub(/.md$/,".html") ]
end
end
def run
2010-11-18 01:13:58 +00:00
i=-1
@filelist.each do |doublon|
2010-11-18 01:13:58 +00:00
i+=1
source=doublon[0]
dest=doublon[1]
puts source
# read and compile in LaTeX the .md file
2010-11-18 18:00:25 +00:00
templateindex=2
2010-11-18 01:13:58 +00:00
if (i+1)<@filelist.size
2010-11-18 18:00:25 +00:00
@postfilters[templateindex].nextURL = '/' + @filelist[i + 1][1].gsub('site/','')
2010-11-18 01:13:58 +00:00
else
2010-11-18 18:00:25 +00:00
@postfilters[templateindex].nextURL = "#"
2010-11-18 01:13:58 +00:00
end
if (i-1)>=0
2010-11-18 18:00:25 +00:00
@postfilters[templateindex].prevURL = '/' + @filelist[i - 1][1].gsub('site/','')
2010-11-18 01:13:58 +00:00
else
2010-11-18 18:00:25 +00:00
@postfilters[templateindex].prevURL = "#"
2010-11-18 01:13:58 +00:00
end
text=compile_text( File.new(source,"r").read )
# create directory if necessary
if not FileTest::directory?(File.dirname(dest))
FileUtils.mkdir_p(File.dirname(dest))
end
# write the .tex file
fic = File.new(dest,"w")
fic.write(text)
fic.close
end
# write the .tex file containing all includes
process_template
2010-11-18 01:13:58 +00:00
system("cp -rf include site/")
end
end
KrambookCompile.new.run
end
2010-11-03 23:24:39 +00:00
task :compile do
require 'rubygems'
require 'kramdown'
require 'filters/markdown_macros'
2010-11-05 14:45:28 +00:00
require 'filters/mkd_post_latex_macros'
2010-11-03 23:24:39 +00:00
2010-11-09 10:32:47 +00:00
class KrambookCompile
require 'config.rb'
attr_accessor :filelist
# take a string from kramdown
# returns LaTeX after filter
def compile_text(tmp)
@prefilters.each do |f|
tmp=f.run( tmp )
end
# compile to latex
tmp=Kramdown::Document.new(tmp, :latex_headers => %w(chapter section subsection paragraph subparagraph subsubparagraph)).to_latex
# post filters
@postfilters.each{ |f| tmp=f.run(tmp) }
return tmp
2010-11-03 23:24:39 +00:00
end
2010-11-09 10:32:47 +00:00
def process_template
2010-11-09 10:58:15 +00:00
template=File.new(@template_file,"r")
2010-11-09 10:32:47 +00:00
txt=template.read
template.close
txt.sub!( /%%#INCLUDES#%%/ ) do
@filelist.map do |source,dest|
"\\include{#{dest.sub(/^tmp\//,'').sub(/.tex/,'')}}"
end.join("\n")
end.
sub!(%{\\author\{\}},'\author{'+@author+'}').
2010-11-09 10:58:15 +00:00
sub!(%{\\title\{\}},'\title{'+@title+'}').
sub!( /%%# LATEX HEADER FROM config\.rb #%%/,@latex_headers)
2010-11-09 10:32:47 +00:00
fic=File.new("tmp/#{@pdfname}.tex","w")
fic.write(txt)
fic.close
end
def initialize
eval File.new('config.rb','r').read
@prefilters=[]
@prefilters<<=MarkdownMacros.new
@postfilters=[]
@postfilters<<=MarkdownPostLatexMacros.new
@filelist=Dir.glob("content/**/*.md").sort.map do |fic|
[ fic, fic.sub(/^content\//,"tmp/").sub(/.md$/,".tex") ]
end
end
def run
@filelist.each do |doublon|
source=doublon[0]
dest=doublon[1]
puts source
# read and compile in LaTeX the .md file
text=compile_text( File.new(source,"r").read )
# create directory if necessary
if not FileTest::directory?(File.dirname(dest))
FileUtils.mkdir_p(File.dirname(dest))
end
# write the .tex file
fic = File.new(dest,"w")
fic.write(text)
fic.close
end
# write the .tex file containing all includes
process_template
# launch the xelatex process
system("cp -rf include tmp/")
2010-11-09 10:58:15 +00:00
2010-11-09 10:32:47 +00:00
system("cd tmp; xelatex #{@pdfname}; cd ..")
# on Ubuntu replace by
# system("gnome-open #{@pdfname}.pdf")
# make a symbolic link in order to
# let the tempory files into tmp/
if not FileTest::exists?("#{@pdfname}.pdf")
system("ln -s tmp/#{@pdfname}.pdf #{@pdfname}.pdf")
end
# open the pdf
system("open tmp/#{@pdfname}.pdf")
end
2010-11-03 23:24:39 +00:00
end
2010-11-09 10:32:47 +00:00
KrambookCompile.new.run
2010-11-03 23:24:39 +00:00
end