Skip to content

Commit

Permalink
Merge pull request #493 from kmuto/converter
Browse files Browse the repository at this point in the history
use ReVIEW::Converter instead of system("review-compile ...")
  • Loading branch information
takahashim committed Mar 17, 2016
2 parents 93425c0 + 20d5667 commit 9d9958d
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 22 deletions.
3 changes: 3 additions & 0 deletions lib/review/configure.rb
Expand Up @@ -37,6 +37,9 @@ def self.values
"debug" => nil, # debug flag
"catalogfile" => 'catalog.yml',
"language" => 'ja', # XXX default language should be JA??
"mathml" => nil, # for HTML
"htmlext" => "html",
"htmlversion" => 4,

"chapter_file" => 'CHAPS',
"part_file" => 'PART',
Expand Down
27 changes: 27 additions & 0 deletions lib/review/converter.rb
@@ -0,0 +1,27 @@
# encoding: utf-8
#
# This program is free software.
# You can distribute or modify this program under the terms of
# the GNU LGPL, Lesser General Public License version 2.1.
#

module ReVIEW
class Converter
attr_accessor :target

def initialize(book, builder)
@book = book
@compiler = ReVIEW::Compiler.new(builder)
end

def convert(file, output_path)
chap_name = File.basename(file, '.*')
chap = @book.chapter(chap_name)
result = @compiler.compile(chap)
File.open(output_path, 'w') do |f|
f.puts result
end
end
end
end

47 changes: 31 additions & 16 deletions lib/review/epubmaker.rb
Expand Up @@ -12,6 +12,8 @@
require 'rexml/streamlistener'
require 'epubmaker'
require 'review/htmltoc'
require 'review/converter'
require 'review/htmlbuilder'

module ReVIEW
class EPUBMaker
Expand Down Expand Up @@ -178,6 +180,13 @@ def recursive_copy_files(resdir, destdir, allow_exts)
end
end

def check_compile_status
return unless @compile_errors

$stderr.puts "compile error, No EPUB file output."
exit 1
end

def build_body(basetmpdir, yamlfile)
@precount = 0
@bodycount = 0
Expand All @@ -188,15 +197,17 @@ def build_body(basetmpdir, yamlfile)
@tocdesc = Array.new
# toccount = 2 ## not used

basedir = Dir.pwd
basedir = File.dirname(yamlfile)
base_path = Pathname.new(basedir)
book = ReVIEW::Book.load(basedir)
book.load_config(yamlfile)
book.config = @params
@converter = ReVIEW::Converter.new(book, ReVIEW::HTMLBuilder.new)
@compile_errors = nil
book.parts.each do |part|
htmlfile = nil
if part.name.present?
if part.file?
build_chap(part, base_path, basetmpdir, yamlfile, true)
build_chap(part, base_path, basetmpdir, true)
else
htmlfile = "part_#{part.number}.#{@params["htmlext"]}"
build_part(part, basetmpdir, htmlfile)
Expand All @@ -208,10 +219,11 @@ def build_body(basetmpdir, yamlfile)
end

part.chapters.each do |chap|
build_chap(chap, base_path, basetmpdir, yamlfile, nil)
build_chap(chap, base_path, basetmpdir, false)
end

end
check_compile_status()
end

def build_part(part, basetmpdir, htmlfile)
Expand Down Expand Up @@ -241,11 +253,11 @@ def template_name
end
end

def build_chap(chap, base_path, basetmpdir, yamlfile, ispart=nil)
def build_chap(chap, base_path, basetmpdir, ispart)
filename = ""

chaptype = "body"
if ispart.present?
if ispart
chaptype = "part"
elsif chap.on_PREDEF?
chaptype = "pre"
Expand Down Expand Up @@ -277,8 +289,6 @@ def build_chap(chap, base_path, basetmpdir, yamlfile, ispart=nil)
write_buildlogtxt(basetmpdir, htmlfile, filename)
log("Create #{htmlfile} from #{filename}.")

level = @params["secnolevel"]

# TODO: It would be nice if we can modify level in PART, PREDEF, or POSTDEF.
# But we have to care about section number reference (@<hd>) also.
#
Expand All @@ -289,15 +299,20 @@ def build_chap(chap, base_path, basetmpdir, yamlfile, ispart=nil)
# level = @params["post_secnolevel"] if chap.on_APPENDIX?
# end

stylesheet = ""
if @params["stylesheet"].size > 0
stylesheet = "--stylesheet=#{@params["stylesheet"].join(",")}"
if @params["params"].present?
warn "'params:' in config.yml is obsoleted."
if @params["params"] =~ /stylesheet=/
warn "stylesheets should be defined in 'stylesheet:', not in 'params:'"
end
end
begin
@converter.convert(filename, File.join(basetmpdir, htmlfile))
write_info_body(basetmpdir, id, htmlfile, ispart, chaptype)
rescue => e
@compile_errors = true
warn "compile error in #{filename} (#{e.class})"
warn e.message
end

ENV["REVIEWFNAME"] = filename
system("#{ReVIEW::MakerHelper.bindir}/review-compile --yaml=#{yamlfile} --target=html --level=#{level} --htmlversion=#{@params["htmlversion"]} --epubversion=#{@params["epubversion"]} #{stylesheet} #{@params["params"]} #{filename} > \"#{basetmpdir}/#{htmlfile}\"")

write_info_body(basetmpdir, id, htmlfile, ispart, chaptype)
end

def detect_properties(path)
Expand Down
16 changes: 10 additions & 6 deletions lib/review/pdfmaker.rb
Expand Up @@ -14,6 +14,8 @@

require 'review'
require 'review/i18n'
require 'review/converter'
require 'review/latexbuilder'


module ReVIEW
Expand All @@ -25,7 +27,7 @@ class PDFMaker
attr_accessor :config, :basedir

def initialize
@basedir = Dir.pwd
@basedir = nil
end

def system_or_raise(*args)
Expand Down Expand Up @@ -101,6 +103,7 @@ def execute(*args)
# YAML configs will be overridden by command line options.
@config.merge!(cmd_config)
I18n.setup(@config["language"])
@basedir = File.dirname(yamlfile)
generate_pdf(yamlfile)
end

Expand All @@ -114,6 +117,7 @@ def generate_pdf(yamlfile)

book = ReVIEW::Book.load(@basedir)
book.config = @config
@converter = ReVIEW::Converter.new(book, ReVIEW::LATEXBuilder.new)
book.parts.each do |part|
if part.name.present?
if part.file?
Expand Down Expand Up @@ -193,12 +197,12 @@ def generate_pdf(yamlfile)

def output_chaps(filename, yamlfile)
$stderr.puts "compiling #{filename}.tex"
cmd = "#{ReVIEW::MakerHelper.bindir}/review-compile --yaml=#{yamlfile} --target=latex --level=#{@config["secnolevel"]} --toclevel=#{@config["toclevel"]} #{@config["params"]} #{filename}.re > #{@path}/#{filename}.tex"
if system cmd
# OK
else
begin
@converter.convert(filename+".re", File.join(@path, filename+".tex"))
rescue => e
@compile_errors = true
warn cmd
warn "compile error in #{filename}.tex (#{e.class})"
warn e.message
end
end

Expand Down
3 changes: 3 additions & 0 deletions test/test_pdfmaker.rb
Expand Up @@ -37,6 +37,7 @@ def test_check_book_existed
def test_check_book_none
Dir.mktmpdir do |dir|
assert_nothing_raised do
@maker.basedir = Dir.pwd
@maker.remove_old_file
end
end
Expand Down Expand Up @@ -132,6 +133,7 @@ def test_make_okuduke_dojin

def test_gettemplate
Dir.mktmpdir do |dir|
@maker.basedir = Dir.pwd
tmpl = @maker.get_template
expect = File.read(File.join(assets_dir,"test_template.tex"))
assert_equal(expect, tmpl)
Expand Down Expand Up @@ -159,6 +161,7 @@ def test_gettemplate_with_backmatter

expect = File.read(File.join(assets_dir,"test_template_backmatter.tex"))

@maker.basedir = Dir.pwd
tmpl = @maker.get_template
tmpl.gsub!(/\A.*%% backmatter begins\n/m,"")
assert_equal(expect, tmpl)
Expand Down

0 comments on commit 9d9958d

Please sign in to comment.