Skip to content

Commit

Permalink
Merge pull request #1694 from kmuto/1674-logger
Browse files Browse the repository at this point in the history
`warn` and `error` have been completely replaced.
  • Loading branch information
takahashim authored May 25, 2021
2 parents 978385d + 03e7784 commit 8ce8e58
Show file tree
Hide file tree
Showing 30 changed files with 508 additions and 421 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ Style/MethodCallWithArgsParentheses:
- 'write'
- 'warn'
- 'error'
- 'error!'
- 'app_error'
- 'raise'
- 'yield'
- 'source'
Expand Down
23 changes: 8 additions & 15 deletions bin/review-compile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ require 'fileutils'
require 'optparse'
require 'yaml'

include ReVIEW::Loggable

DEFAULT_CONFIG_FILENAME = 'config.yml'.freeze

def main
Expand Down Expand Up @@ -47,17 +49,17 @@ def _main
begin
loader = ReVIEW::YAMLLoader.new
if @config['yaml']
error "#{@config['yaml']} not found." unless File.exist?(@config['yaml'])
error! "#{@config['yaml']} not found." unless File.exist?(@config['yaml'])
begin
@config.deep_merge!(loader.load_file(@config['yaml']))
rescue => e
error "yaml error #{e.message}"
error! "yaml error #{e.message}"
end
elsif File.exist?(DEFAULT_CONFIG_FILENAME)
begin
@config.deep_merge!(loader.load_file(DEFAULT_CONFIG_FILENAME))
rescue => e
error "yaml error #{e.message}"
error! "yaml error #{e.message}"
end
end

Expand All @@ -73,12 +75,12 @@ def _main

case @mode
when :files
error('no input') if ARGV.empty?
error!('no input') if ARGV.empty?

@basedir = File.dirname(ARGV[0])
book = ReVIEW::Book::Base.new(@basedir, config: @config)
ARGV.each do |item|
error("file not found: #{item}") unless File.exist?(File.join(book.config['contentdir'], item))
error!("file not found: #{item}") unless File.exist?(File.join(book.config['contentdir'], item))
chap_name = File.basename(item, '.*')
chap = book.chapter(chap_name)
compiler = ReVIEW::Compiler.new(load_builder_class(@target, @check_only))
Expand All @@ -105,7 +107,7 @@ def _main
raise "must not happen: #{@mode}"
end
rescue ReVIEW::ApplicationError => e
error(e.message)
error! e.message
end
end

Expand Down Expand Up @@ -164,15 +166,6 @@ def parse_opts
end
end

def error(msg)
@logger.error msg
exit 1
end

def warn(msg)
@logger.warn msg
end

def load_builder_class(target, strict)
require "review/#{target}builder"
ReVIEW.const_get("#{target.upcase}Builder").new(strict)
Expand Down
61 changes: 26 additions & 35 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'review/compiler'
require 'review/sec_counter'
require 'review/img_math'
require 'review/loggable'
require 'stringio'
require 'fileutils'
require 'tempfile'
Expand All @@ -19,6 +20,7 @@
module ReVIEW
class Builder
include TextUtils
include Loggable

CAPTION_TITLES = Compiler.minicolumn_names

Expand All @@ -31,6 +33,7 @@ def post_paragraph
end

attr_accessor :doc_status, :previous_list_type
attr_reader :location

def initialize(strict = false, *_args, img_math: nil)
@strict = strict
Expand Down Expand Up @@ -76,7 +79,7 @@ def bind(compiler, chapter, location)
begin
require 'unicode/eaw'
rescue LoadError
warn 'not found unicode/eaw. disabled join_lines_by_lang feature.'
warn 'not found unicode/eaw. disabled join_lines_by_lang feature.', location: @location
@book.config['join_lines_by_lang'] = nil
end
end
Expand All @@ -101,7 +104,7 @@ def solve_nest(s)

def check_nest
if @children && !@children.empty?
error "//beginchild of #{@children.reverse.join(',')} misses //endchild"
app_error "//beginchild of #{@children.reverse.join(',')} misses //endchild"
end
end

Expand Down Expand Up @@ -161,7 +164,7 @@ def list(lines, id, caption, lang = nil)
list_body(id, lines, lang)
list_header(id, caption, lang) unless caption_top?('list')
rescue KeyError
error "no such list: #{id}"
app_error "no such list: #{id}"
end
end

Expand All @@ -171,7 +174,7 @@ def listnum(lines, id, caption, lang = nil)
listnum_body(lines, lang)
list_header(id, caption, lang) unless caption_top?('list')
rescue KeyError
error "no such list: #{id}"
app_error "no such list: #{id}"
end
end

Expand All @@ -185,7 +188,7 @@ def image(lines, id, caption, metric = nil)
if @chapter.image_bound?(id)
image_image(id, caption, metric)
else
warn "image not bound: #{id}" if @strict
warn "image not bound: #{id}", location: @location if @strict
image_dummy(id, caption, lines)
end
end
Expand All @@ -203,7 +206,7 @@ def table(lines, id = nil, caption = nil)
table_header(id, caption)
end
rescue KeyError
error "no such table: #{id}"
app_error "no such table: #{id}"
end
end

Expand All @@ -218,7 +221,7 @@ def table_row_separator_regexp
when 'verticalbar'
Regexp.new('\s*\\' + escape('|') + '\s*')
else
error "Unknown value for 'table_row_separator', shold be: tabs, singletab, spaces, verticalbar"
app_error "Unknown value for 'table_row_separator', shold be: tabs, singletab, spaces, verticalbar"
end
end

Expand All @@ -233,7 +236,7 @@ def parse_table_rows(lines)
rows.push(line.strip.split(table_row_separator_regexp).map { |s| s.sub(/\A\./, '') })
end
rows = adjust_n_cols(rows)
error 'no rows in the table' if rows.empty?
app_error 'no rows in the table' if rows.empty?
[sepidx, rows]
end

Expand Down Expand Up @@ -294,19 +297,19 @@ def compile_inline(s)
def inline_chapref(id)
compile_inline(@book.chapter_index.display_string(id))
rescue KeyError
error "unknown chapter: #{id}"
app_error "unknown chapter: #{id}"
end

def inline_chap(id)
@book.chapter_index.number(id)
rescue KeyError
error "unknown chapter: #{id}"
app_error "unknown chapter: #{id}"
end

def inline_title(id)
compile_inline(@book.chapter_index.title(id))
rescue KeyError
error "unknown chapter: #{id}"
app_error "unknown chapter: #{id}"
end

def inline_list(id)
Expand All @@ -317,7 +320,7 @@ def inline_list(id)
%Q(#{I18n.t('list')}#{I18n.t('format_number_without_chapter', [chapter.list(id).number])})
end
rescue KeyError
error "unknown list: #{id}"
app_error "unknown list: #{id}"
end

def inline_img(id)
Expand All @@ -328,7 +331,7 @@ def inline_img(id)
%Q(#{I18n.t('image')}#{I18n.t('format_number_without_chapter', [chapter.image(id).number])})
end
rescue KeyError
error "unknown image: #{id}"
app_error "unknown image: #{id}"
end

def inline_imgref(id)
Expand All @@ -349,7 +352,7 @@ def inline_table(id)
%Q(#{I18n.t('table')}#{I18n.t('format_number_without_chapter', [chapter.table(id).number])})
end
rescue KeyError
error "unknown table: #{id}"
app_error "unknown table: #{id}"
end

def inline_eq(id)
Expand All @@ -360,13 +363,13 @@ def inline_eq(id)
%Q(#{I18n.t('equation')}#{I18n.t('format_number_without_chapter', [chapter.equation(id).number])})
end
rescue KeyError
error "unknown equation: #{id}"
app_error "unknown equation: #{id}"
end

def inline_fn(id)
@chapter.footnote(id).content
rescue KeyError
error "unknown footnote: #{id}"
app_error "unknown footnote: #{id}"
end

def inline_bou(str)
Expand Down Expand Up @@ -422,7 +425,7 @@ def inline_hd(id)
inline_hd_chap(@chapter, id)
end
rescue KeyError
error "unknown headline: #{id}"
app_error "unknown headline: #{id}"
end

def inline_column(id)
Expand All @@ -436,7 +439,7 @@ def inline_column(id)
inline_column_chap(@chapter, id)
end
rescue KeyError
error "unknown column: #{id}"
app_error "unknown column: #{id}"
end

def inline_column_chap(chapter, id)
Expand All @@ -460,7 +463,7 @@ def inline_w(s)
if translated
escape(translated)
else
warn "word not bound: #{s}"
warn "word not bound: #{s}", location: @location
escape("[missing word: #{s}]")
end
end
Expand Down Expand Up @@ -496,18 +499,6 @@ def embed(lines, arg = nil)
end
end

def warn(msg)
@logger.warn "#{@location}: #{msg}"
end

def error(msg)
if msg =~ /:\d+: error: /
raise ApplicationError, msg
else
raise ApplicationError, "#{@location}: error: #{msg}"
end
end

def handle_metric(str)
str
end
Expand Down Expand Up @@ -582,7 +573,7 @@ def #{name}_end

def check_nested_minicolumn
if @doc_status[:minicolumn]
error "#{@location}: nested mini-column is not allowed"
app_error "#{@location}: nested mini-column is not allowed"
end
end

Expand Down Expand Up @@ -735,23 +726,23 @@ def escape(str)
def beginchild
@children ||= []
unless @previous_list_type
error "//beginchild is shown, but previous element isn't ul, ol, or dl"
app_error "//beginchild is shown, but previous element isn't ul, ol, or dl"
end
puts "\x01#{@previous_list_type}\x01"
@children.push(@previous_list_type)
end

def endchild
if @children.nil? || @children.empty?
error "//endchild is shown, but any opened //beginchild doesn't exist"
app_error "//endchild is shown, but any opened //beginchild doesn't exist"
else
puts "\x01→/#{@children.pop}\x01"
end
end

def caption_top?(type)
unless %w[top bottom].include?(@book.config['caption_position'][type])
warn("invalid caption_position/#{type} parameter. 'top' is assumed")
warn "invalid caption_position/#{type} parameter. 'top' is assumed", location: @location
end
@book.config['caption_position'][type] != 'bottom'
end
Expand Down
Loading

0 comments on commit 8ce8e58

Please sign in to comment.