Class: ReVIEW::TOCPrinter
Direct Known Subclasses
Defined Under Namespace
Classes: Counter
Instance Attribute Summary collapse
-
#calc_char_width ⇒ Object
Returns the value of attribute calc_char_width.
Class Method Summary collapse
Instance Method Summary collapse
- #build_chap(chap) ⇒ Object
- #build_result_array ⇒ Object
- #calc_line_wrapping(line, mode:) ⇒ Object
- #calc_linesize(line) ⇒ Object
- #calc_pages(result) ⇒ Object
- #calc_total_count(name, headline_array) ⇒ Object
- #execute(*args) ⇒ Object
-
#initialize ⇒ TOCPrinter
constructor
A new instance of TOCPrinter.
- #parse_contents(name, upper, content) ⇒ Object
- #parse_options(args) ⇒ Object
- #print_result(result_array) ⇒ Object
Constructor Details
#initialize ⇒ TOCPrinter
Returns a new instance of TOCPrinter.
58 59 60 61 62 63 64 65 66 |
# File 'lib/review/tocprinter.rb', line 58 def initialize @logger = ReVIEW.logger @yamlfile = 'config.yml' @upper = 4 @indent = true @buildonly = nil @detail = nil @calc_char_width = nil end |
Instance Attribute Details
#calc_char_width ⇒ Object
Returns the value of attribute calc_char_width.
68 69 70 |
# File 'lib/review/tocprinter.rb', line 68 def calc_char_width @calc_char_width end |
Class Method Details
.execute(*args) ⇒ Object
54 55 56 |
# File 'lib/review/tocprinter.rb', line 54 def self.execute(*args) new.execute(*args) end |
Instance Method Details
#build_chap(chap) ⇒ Object
259 260 261 262 263 264 265 266 267 |
# File 'lib/review/tocprinter.rb', line 259 def build_chap(chap) compiler = ReVIEW::Compiler.new(ReVIEW::PLAINTEXTTocBuilder.new) begin compiler.compile(@book.chapter(chap.name)) rescue ReVIEW::ApplicationError => e @logger.error e. '' end end |
#build_result_array ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/review/tocprinter.rb', line 98 def build_result_array result_array = [] begin @book.parts.each do |part| if part.name.present? && (@buildonly.nil? || @buildonly.include?(part.name)) result_array.push(Counter.new(part: 'start')) if part.file? content = build_chap(part) result_array.concat(parse_contents(part.name, @upper, content)) else title = part.format_number + I18n.t('chapter_postfix') + part.title result_array.push( Counter.new(name: '', lines: 1, chars: title.size, list_lines: 0, text_lines: 1), Counter.new(level: 0, headline: title, lines: 1, chars: title.size, list_lines: 0, text_lines: 1) ) end end part.chapters.each do |chap| if @buildonly.nil? || @buildonly.include?(chap.name) content = build_chap(chap) result_array.concat(parse_contents(chap.name, @upper, content)) end end if part.name.present? && (@buildonly.nil? || @buildonly.include?(part.name)) result_array.push(Counter.new(part: 'end')) end end rescue ReVIEW::FileNotFound, ReVIEW::CompileError => e @logger.error e exit 1 end result_array end |
#calc_line_wrapping(line, mode:) ⇒ Object
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/review/tocprinter.rb', line 229 def calc_line_wrapping(line, mode:) return 1 if line.size == 0 case mode when :list ((calc_linesize(line) - 1) / @book.page_metric.list.n_columns) + 1 else # mode == :text ((calc_linesize(line) - 1) / @book.page_metric.text.n_columns) + 1 end end |
#calc_linesize(line) ⇒ Object
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/review/tocprinter.rb', line 166 def calc_linesize(line) return line.size unless @calc_char_width line.each_char.inject(0) do |result, char| # XXX: should include A also? if %i[Na H N].include?(Unicode::Eaw.property(char)) result + 0.5 # halfwidth else result + 1 end end end |
#calc_pages(result) ⇒ Object
161 162 163 164 |
# File 'lib/review/tocprinter.rb', line 161 def calc_pages(result) (result.list_lines.to_f / @book.page_metric.list.n_lines) + (result.text_lines.to_f / @book.page_metric.text.n_lines) end |
#calc_total_count(name, headline_array) ⇒ Object
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/review/tocprinter.rb', line 240 def calc_total_count(name, headline_array) total = Counter.new(name: name, lines: 0, chars: 0, list_lines: 0, text_lines: 0) headline_array.each do |h| next unless h.lines total.lines += h.lines total.chars += h.chars total.list_lines += h.list_lines total.text_lines += h.text_lines end total end |
#execute(*args) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/review/tocprinter.rb', line 70 def execute(*args) (args) begin @config = ReVIEW::Configure.create(yamlfile: @yamlfile) @book = ReVIEW::Book::Base.new('.', config: @config) unless File.readable?(@yamlfile) raise ReVIEW::FileNotFound, "No such fiile or can't open #{@yamlfile}." end rescue ReVIEW::ConfigError, ReVIEW::FileNotFound, ReVIEW::CompileError, ReVIEW::ApplicationError => e @logger.error e. exit 1 end I18n.setup(@config['language']) if @detail begin require 'unicode/eaw' @calc_char_width = true rescue LoadError @logger.warn('not found unicode/eaw library. page volume may be unreliable.') @calc_char_width = nil end end print_result(build_result_array) end |
#parse_contents(name, upper, content) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/review/tocprinter.rb', line 179 def parse_contents(name, upper, content) headline_array = [] counter = Counter.new(lines: 0, chars: 0, list_lines: 0, text_lines: 0) listmode = nil content.each_line(chomp: true) do |l| if l.start_with?("\x01STARTLIST\x01") listmode = true next elsif l.start_with?("\x01ENDLIST\x01") listmode = nil next elsif l =~ /\A\x01H(\d)\x01/ # headline level = $1.to_i l = $' if level <= upper if counter.chars > 0 headline_array.push(counter) end headline = l counter = Counter.new( level: level, headline: headline, lines: 1, chars: headline.size, list_lines: 0, text_lines: 1 ) next end end counter.lines += 1 counter.chars += l.size if listmode # code list: calculate line wrapping counter.list_lines += calc_line_wrapping(l, mode: :list) else # normal paragraph: calculate line wrapping counter.text_lines += calc_line_wrapping(l, mode: :text) end end headline_array.push(counter) total = calc_total_count(name, headline_array) headline_array.unshift(total) end |
#parse_options(args) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/review/tocprinter.rb', line 269 def (args) opts = OptionParser.new opts.version = ReVIEW::VERSION opts.on('--yaml=YAML', 'Read configurations from YAML file.') { |yaml| @yamlfile = yaml } opts.on('-y', '--only file1,file2,...', 'list only specified files.') do |v| @buildonly = v.split(/\s*,\s*/).map { |m| m.strip.sub(/\.re\Z/, '') } end opts.on('-l', '--level N', 'list upto N level (default=4)') do |n| @upper = n.to_i end opts.on('-d', '--detail', 'show characters and lines of each section.') do @detail = true end opts.on('--noindent', "don't indent headlines.") do @indent = nil end opts.on('--help', 'print this message and quit.') do puts opts.help exit 0 end begin opts.parse!(args) rescue OptionParser::ParseError => e @logger.error e. $stderr.puts opts.help exit 1 end end |
#print_result(result_array) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/review/tocprinter.rb', line 134 def print_result(result_array) result_array.each do |result| if result.part next end if result.name # file information if @detail puts '=============================' printf("%6dC %5dL %5dP %s\n", result.chars, result.lines, calc_pages(result).ceil, result.name) puts '-----------------------------' end next end # section information if @detail printf('%6dC %5dL %5.1fP ', result.chars, result.lines, calc_pages(result)) end if @indent && result.level print ' ' * (result.level == 0 ? 0 : result.level - 1) end puts result.headline end end |