Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed Aug 20, 2019
2 parents 46ecfa7 + 8736d96 commit b31c530
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 182 deletions.
30 changes: 13 additions & 17 deletions lib/review/book/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ def initialize(basedir = '.')
@chapter_index = nil
@config = ReVIEW::Configure.values
@catalog = nil
@read_part = nil
@warn_old_files = {} # XXX for checking CHAPS, PREDEF, POSTDEF
@basedir_seen = {}
update_rubyenv
Expand Down Expand Up @@ -202,41 +201,39 @@ def read_chaps
if catalog
catalog.chaps
else
read_file(config['chapter_file'])
read_file(config['chapter_file']).split("\n")
end
end

def read_predef
if catalog
catalog.predef
else
read_file(config['predef_file'])
read_file(config['predef_file']).split("\n")
end
end

def read_appendix
if catalog
catalog.appendix
else
read_file(config['postdef_file']) # for backward compatibility
read_file(config['postdef_file']).split("\n") # for backward compatibility
end
end

def read_postdef
if catalog
catalog.postdef
else
''
[]
end
end

def read_part
return @read_part if @read_part

if catalog
@read_part = catalog.parts
catalog.parts
else
@read_part = File.read(File.join(@basedir, config['part_file']))
File.read(File.join(@basedir, config['part_file'])).split("\n")
end
end

Expand All @@ -258,7 +255,7 @@ def bib_exist?

def prefaces
if catalog
return mkpart_from_namelist(catalog.predef.split("\n"))
return mkpart_from_namelist(catalog.predef)
end

begin
Expand All @@ -273,7 +270,7 @@ def prefaces

def appendix
if catalog
names = catalog.appendix.split("\n")
names = catalog.appendix
chaps = names.each_with_index.map { |n, idx| mkchap_ifexist(n, idx) }.compact
return mkpart(chaps)
end
Expand All @@ -290,7 +287,7 @@ def appendix

def postscripts
if catalog
mkpart_from_namelist(catalog.postdef.split("\n"))
mkpart_from_namelist(catalog.postdef)
end
end

Expand Down Expand Up @@ -324,7 +321,7 @@ def parse_chapters
chap = Chapter.new(self, num += 1, chap, File.join(contentdir, chap))
chap
end
Part.new(self, part += 1, chaps, read_part.split("\n")[part - 1])
Part.new(self, part += 1, chaps, read_part[part - 1])
else
chap = Chapter.new(self, num += 1, entry, File.join(contentdir, entry))
if chap.number
Expand All @@ -337,12 +334,11 @@ def parse_chapters
end
end

chap = read_chaps.
strip.lines.map(&:strip).join("\n").split(/\n{2,}/).
chap = read_chaps.map(&:strip).join("\n").split(/\n{2,}/).
map do |part_chunk|
chaps = part_chunk.split.map { |chapid| Chapter.new(self, num += 1, chapid, File.join(contentdir, chapid)) }
if part_exist? && read_part.split("\n").size > part
Part.new(self, part += 1, chaps, read_part.split("\n")[part - 1])
if part_exist? && read_part.size > part
Part.new(self, part += 1, chaps, read_part[part - 1])
else
Part.new(self, nil, chaps)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/review/book/chapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def on_postdef?
private

def on_file?(contents)
contents.lines.map(&:strip).include?("#{id}#{@book.ext}")
contents.map(&:strip).include?("#{id}#{@book.ext}")
end

# backward compatibility
Expand Down
29 changes: 17 additions & 12 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ def builder_init_file
end
private :builder_init_file

def highlight?
false
end

def result
@output.string
end
Expand Down Expand Up @@ -162,7 +166,7 @@ def table(lines, id = nil, caption = nil)
error "no such table: #{id}"
end
table_begin(rows.first.size)
table_tr(sepidx, rows)
table_rows(sepidx, rows)
table_end
end

Expand All @@ -181,7 +185,7 @@ def parse_table_rows(lines)
[sepidx, rows]
end

def table_tr(sepidx, rows)
def table_rows(sepidx, rows)
if sepidx
sepidx.times do
tr(rows.shift.map { |s| th(s) })
Expand Down Expand Up @@ -410,7 +414,12 @@ def inline_w(s)
end

def inline_wb(s)
inline_b(unescape(inline_w(s)))
translated = @dictionary[s]
if translated
inline_b(translated)
else
inline_b("[missing word: #{s}]")
end
end

def raw(str)
Expand All @@ -429,9 +438,9 @@ def embed(lines, arg = nil)
if arg
builders = arg.gsub(/^\s*\|/, '').gsub(/\|\s*$/, '').gsub(/\s/, '').split(',')
c = target_name
print lines.join if builders.include?(c)
print lines.join("\n") + "\n" if builders.include?(c)
else
print lines.join
print lines.join("\n") + "\n"
end
end

Expand Down Expand Up @@ -510,13 +519,13 @@ def graph(lines, id, command, caption = '')
file = "#{id}.#{image_ext}"
file_path = File.join(dir, file)

line = self.unescape(lines.join("\n"))
content = lines.join("\n") + "\n"

tf = Tempfile.new('review_graph')
tf.puts line
tf.puts content
tf.close
begin
file_path = send("graph_#{command}".to_sym, id, file_path, line, tf.path)
file_path = send("graph_#{command}".to_sym, id, file_path, content, tf.path)
ensure
tf.unlink
end
Expand Down Expand Up @@ -640,9 +649,5 @@ def detab(str, num = nil)
def escape(str)
str
end

def unescape(str)
str
end
end
end # module ReVIEW
23 changes: 10 additions & 13 deletions lib/review/catalog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,31 @@ def initialize(file)
end

def predef
return '' unless @yaml['PREDEF']
@yaml['PREDEF'].join("\n")
@yaml['PREDEF'] || []
end

def chaps
return '' unless @yaml['CHAPS']
return [] unless @yaml['CHAPS']

@yaml['CHAPS'].map do |entry|
if entry.is_a?(String)
entry
elsif entry.is_a?(Hash)
entry.values # chaps in a part
end
end.flatten.join("\n")
end.flatten
end

def parts
return '' unless @yaml['CHAPS']
return [] unless @yaml['CHAPS']

part_list = @yaml['CHAPS'].map do |entry|
if entry.is_a?(Hash)
entry.keys
end
end

part_list.flatten.compact.join("\n")
part_list.flatten.compact
end

def replace_part(old_name, new_name)
Expand All @@ -55,13 +54,11 @@ def parts_with_chaps
end

def appendix
return '' unless @yaml['APPENDIX']
@yaml['APPENDIX'].join("\n")
@yaml['APPENDIX'] || []
end

def postdef
return '' unless @yaml['POSTDEF']
@yaml['POSTDEF'].join("\n")
@yaml['POSTDEF'] || []
end

def to_s
Expand All @@ -71,7 +68,7 @@ def to_s
def validate!(config, basedir)
filenames = []
if predef.present?
filenames.concat(predef.split(/\n/))
filenames.concat(predef)
end
parts_with_chaps.each do |chap|
if chap.is_a?(Hash)
Expand All @@ -86,10 +83,10 @@ def validate!(config, basedir)
end
end
if appendix.present?
filenames.concat(appendix.split(/\n/))
filenames.concat(appendix)
end
if postdef.present?
filenames.concat(postdef.split(/\n/))
filenames.concat(postdef)
end
filenames.each do |filename|
refile = File.join(basedir, config['contentdir'], filename)
Expand Down
39 changes: 33 additions & 6 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,25 @@ module ReVIEW
class Compiler
def initialize(strategy)
@strategy = strategy

## commands which do not parse block lines in compiler
@non_parsed_commands = %i[embed texequation graph]

## to decide escaping/non-escaping for text
@command_name_stack = []
end

attr_reader :strategy

def compile(chap)
@chapter = chap
@non_parsed_commands = %i[embed texequation graph]
if @strategy.highlight?
@non_escaped_commands = %i[list emlist listnum emlistnum cmd]
else
@non_escaped_commands = []
end
@command_name_stack = []
do_compile
@strategy.result
end
Expand Down Expand Up @@ -235,14 +248,17 @@ def do_compile
f.gets
error 'block end seen but not opened'
when %r{\A//[a-z]+}
# @command_name_stack.push(name) ## <- move into read_command() to use name
name, args, lines = read_command(f)
syntax = syntax_descriptor(name)
unless syntax
error "unknown command: //#{name}"
compile_unknown_command(args, lines)
@command_name_stack.pop
next
end
compile_command(syntax, args, lines)
@command_name_stack.pop
when %r{\A//}
line = f.gets
warn "`//' seen but is not valid command: #{line.strip.inspect}"
Expand Down Expand Up @@ -422,7 +438,8 @@ def compile_paragraph(f)
def read_command(f)
line = f.gets
name = line.slice(/[a-z]+/).to_sym
ignore_inline = (name == :embed)
ignore_inline = @non_parsed_commands.include?(name)
@command_name_stack.push(name)
args = parse_args(line.sub(%r{\A//[a-z]+}, '').rstrip.chomp('{'), name)
@strategy.doc_status[name] = true
lines = block_open?(line) ? read_block(f, ignore_inline) : nil
Expand All @@ -439,9 +456,9 @@ def read_block(f, ignore_inline)
buf = []
f.until_match(%r{\A//\}}) do |line|
if ignore_inline
buf.push(line)
buf.push(line.chomp)
elsif line !~ /\A\#@/
buf.push(text(line.rstrip))
buf.push(text(line.rstrip, true))
end
end
unless %r{\A//\}} =~ f.peek
Expand Down Expand Up @@ -527,18 +544,28 @@ def revert_replace_fence(str)
str.gsub("\x01", '@').gsub("\x02", '\\').gsub("\x03", '{').gsub("\x04", '}')
end

def text(str)
def in_non_escaped_command?
current_command = @command_name_stack.last
current_command && @non_escaped_commands.include?(current_command)
end

def text(str, block_mode = false)
return '' if str.empty?
words = replace_fence(str).split(/(@<\w+>\{(?:[^\}\\]|\\.)*?\})/, -1)
words.each do |w|
if w.scan(/@<\w+>/).size > 1 && !/\A@<raw>/.match(w)
error "`@<xxx>' seen but is not valid inline op: #{w}"
end
end
result = @strategy.nofunc_text(revert_replace_fence(words.shift))
result = ''
until words.empty?
if in_non_escaped_command? && block_mode
result << revert_replace_fence(words.shift)
else
result << @strategy.nofunc_text(revert_replace_fence(words.shift))
end
break if words.empty?
result << compile_inline(revert_replace_fence(words.shift.gsub(/\\\}/, '}').gsub(/\\\\/, '\\')))
result << @strategy.nofunc_text(revert_replace_fence(words.shift))
end
result
rescue => e
Expand Down
Loading

0 comments on commit b31c530

Please sign in to comment.