Class: ReVIEW::TEXTMaker
- Includes:
- MakerHelper
- Defined in:
- lib/review/textmaker.rb
Instance Attribute Summary collapse
-
#basedir ⇒ Object
Returns the value of attribute basedir.
-
#config ⇒ Object
Returns the value of attribute config.
Class Method Summary collapse
Instance Method Summary collapse
- #build_body(basetmpdir, _yamlfile) ⇒ Object
- #build_chap(chap, base_path, basetmpdir, ispart) ⇒ Object
- #build_part(part, basetmpdir, textfile) ⇒ Object
- #build_path ⇒ Object
- #error(msg) ⇒ Object
- #execute(*args) ⇒ Object
- #generate_text_files(yamlfile) ⇒ Object
-
#initialize ⇒ TEXTMaker
constructor
A new instance of TEXTMaker.
- #parse_opts(args) ⇒ Object
- #remove_old_files(path) ⇒ Object
- #warn(msg) ⇒ Object
Methods included from MakerHelper
Constructor Details
#initialize ⇒ TEXTMaker
Returns a new instance of TEXTMaker.
27 28 29 30 31 32 |
# File 'lib/review/textmaker.rb', line 27 def initialize @basedir = nil @logger = ReVIEW.logger @plaintext = nil @img_math = nil end |
Instance Attribute Details
#basedir ⇒ Object
Returns the value of attribute basedir.
25 26 27 |
# File 'lib/review/textmaker.rb', line 25 def basedir @basedir end |
#config ⇒ Object
Returns the value of attribute config.
25 26 27 |
# File 'lib/review/textmaker.rb', line 25 def config @config end |
Class Method Details
.execute(*args) ⇒ Object
43 44 45 |
# File 'lib/review/textmaker.rb', line 43 def self.execute(*args) self.new.execute(*args) end |
Instance Method Details
#build_body(basetmpdir, _yamlfile) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/review/textmaker.rb', line 114 def build_body(basetmpdir, _yamlfile) base_path = Pathname.new(@basedir) builder = nil if @plaintext builder = ReVIEW::PLAINTEXTBuilder.new(img_math: @img_math) else builder = ReVIEW::TOPBuilder.new(img_math: @img_math) end @converter = ReVIEW::Converter.new(@book, builder) @book.parts.each do |part| if part.name.present? if part.file? build_chap(part, base_path, basetmpdir, true) else textfile = "part_#{part.number}.txt" build_part(part, basetmpdir, textfile) end end part.chapters.each { |chap| build_chap(chap, base_path, basetmpdir, false) } end end |
#build_chap(chap, base_path, basetmpdir, ispart) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/review/textmaker.rb', line 146 def build_chap(chap, base_path, basetmpdir, ispart) filename = '' if ispart.present? filename = chap.path else filename = Pathname.new(chap.path).relative_path_from(base_path).to_s end id = File.basename(filename).sub(/\.re\Z/, '') if @buildonly && !@buildonly.include?(id) warn "skip #{id}.re" return end textfile = "#{id}.txt" begin @converter.convert(filename, File.join(basetmpdir, textfile)) rescue => e warn "compile error in #{filename} (#{e.class})" warn e. end end |
#build_part(part, basetmpdir, textfile) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/review/textmaker.rb', line 137 def build_part(part, basetmpdir, textfile) File.open(File.join(basetmpdir, textfile), 'w') do |f| f.print '■H1■' unless @plaintext f.print ReVIEW::I18n.t('part', part.number) f.print " #{part.name.strip}" if part.name.strip.present? f.puts end end |
#build_path ⇒ Object
70 71 72 |
# File 'lib/review/textmaker.rb', line 70 def build_path "#{@config['bookname']}-text" end |
#error(msg) ⇒ Object
34 35 36 37 |
# File 'lib/review/textmaker.rb', line 34 def error(msg) @logger.error msg exit 1 end |
#execute(*args) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/review/textmaker.rb', line 79 def execute(*args) cmd_config, yamlfile = parse_opts(args) error "#{yamlfile} not found." unless File.exist?(yamlfile) @config = ReVIEW::Configure.create(maker: 'textmaker', yamlfile: yamlfile, config: cmd_config) @img_math = ReVIEW::ImgMath.new(@config, path_name: '_review_math_text') I18n.setup(@config['language']) begin generate_text_files(yamlfile) @logger.success("built #{build_path}") rescue ApplicationError => e raise if @config['debug'] error(e.) end if @config['math_format'] == 'imgmath' @img_math.make_math_images end end |
#generate_text_files(yamlfile) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/review/textmaker.rb', line 103 def generate_text_files(yamlfile) @basedir = File.dirname(yamlfile) @path = build_path remove_old_files(@path) Dir.mkdir(@path) @book = ReVIEW::Book::Base.new(@basedir, config: @config) build_body(@path, yamlfile) end |
#parse_opts(args) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/review/textmaker.rb', line 47 def parse_opts(args) cmd_config = {} opts = OptionParser.new @buildonly = nil opts. = 'Usage: review-textmaker [-n] configfile' opts.version = ReVIEW::VERSION opts.on('-n', 'No decoration.') { @plaintext = true } opts.on('-y', '--only file1,file2,...', 'Build only specified files.') { |v| @buildonly = v.split(/\s*,\s*/).map { |m| m.strip.sub(/\.re\Z/, '') } } opts.on('--help', 'Prints this message and quit.') do puts opts.help exit 0 end opts.parse!(args) if args.size != 1 puts opts.help exit 0 end [cmd_config, args[0]] end |
#remove_old_files(path) ⇒ Object
74 75 76 77 |
# File 'lib/review/textmaker.rb', line 74 def remove_old_files(path) @img_math.cleanup_mathimg FileUtils.rm_rf(path) end |
#warn(msg) ⇒ Object
39 40 41 |
# File 'lib/review/textmaker.rb', line 39 def warn(msg) @logger.warn msg end |