Skip to content

Commit

Permalink
introduce //child op. Closes: #1492
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed Apr 5, 2020
1 parent ef04b6a commit ce51f1b
Show file tree
Hide file tree
Showing 11 changed files with 1,142 additions and 11 deletions.
34 changes: 32 additions & 2 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2002-2019 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2020 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -91,8 +91,19 @@ def highlight?
false
end

def solve_nest(s)
check_nest
s.gsub(/\x01→.+?←\x01/, '')
end

def check_nest
if @children && !@children.empty?
error @children.reverse.map { |tag| "//child[#{tag}]" }.join(',') + ' miss close tag'
end
end

def result
@output.string
solve_nest(@output.string)
end

alias_method :raw_result, :result
Expand Down Expand Up @@ -680,5 +691,24 @@ def detab(str, num = nil)
def escape(str)
str
end

def child(tag)
@children ||= []
puts "\x01#{tag}\x01"
if tag.start_with?('/')
if @children.empty?
error "#{tag} is shown but there isn't any opened //child"
elsif @children[-1] != tag.sub('/', '')
error "#{tag} is shown but previous '#{@children[-1]}' is not closed yet"
else
@children.pop
end
else
unless %w[dl ol ul].include?(tag)
error "#{tag} is invalid value for //child (dl, ol, or ul)"
end
@children.push(tag)
end
end
end
end # module ReVIEW
1 change: 1 addition & 0 deletions lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def inline_defined?(name)
defsingle :include, 1
defsingle :olnum, 1
defsingle :firstlinenum, 1
defsingle :child, 1

definline :chapref
definline :chap
Expand Down
18 changes: 16 additions & 2 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2008-2019 Minero Aoki, Kenshi Muto, Masayoshi Takahashi,
# Copyright (c) 2008-2020 Minero Aoki, Kenshi Muto, Masayoshi Takahashi,
# KADO Masanori
# 2002-2007 Minero Aoki
#
Expand Down Expand Up @@ -92,7 +92,7 @@ def layoutfile
def result
# default XHTML header/footer
@title = strip_html(compile_inline(@chapter.title))
@body = @output.string
@body = solve_nest(@output.string)
@language = @book.config['language']
@stylesheets = @book.config['stylesheet']
@next = @chapter.next_chapter
Expand All @@ -107,6 +107,20 @@ def result
ReVIEW::Template.load(layoutfile).result(binding)
end

def solve_nest(s)
check_nest
s.gsub("</dd>\n</dl>\n\x01→dl←\x01", '').
gsub("\x01→/dl←\x01", "</dd>\n</dl>←END\x01").
gsub("</li>\n</ul>\n\x01→ul←\x01", '').
gsub("\x01→/ul←\x01", "</li>\n</ul>←END\x01").
gsub("</li>\n</ol>\n\x01→ol←\x01", '').
gsub("\x01→/ol←\x01", "</li>\n</ol>←END\x01").
gsub("</dl>←END\x01\n<dl>", '').
gsub("</ul>←END\x01\n<ul>", '').
gsub("</ol>←END\x01\n<ol>", '').
gsub("←END\x01", '')
end

def xmlns_ops_prefix
if @book.config['epubversion'].to_i == 3
'epub'
Expand Down
18 changes: 16 additions & 2 deletions lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2008-2019 Minero Aoki, Kenshi Muto
# Copyright (c) 2008-2020 Minero Aoki, Kenshi Muto
# 2002-2007 Minero Aoki
#
# This program is free software.
Expand Down Expand Up @@ -89,7 +89,21 @@ def result
s += '</sect>' if @section > 0
s += '</chapter>' if @chapter.number > 0
end
@output.string + s + "</#{@rootelement}>\n"
solve_nest(@output.string) + s + "</#{@rootelement}>\n"
end

def solve_nest(s)
check_nest
s.gsub("</dd></dl>\x01→dl←\x01", '').
gsub("\x01→/dl←\x01", "</dd></dl>←END\x01").
gsub("</li></ul>\x01→ul←\x01", '').
gsub("\x01→/ul←\x01", "</li></ul>←END\x01").
gsub("</li></ol>\x01→ol←\x01", '').
gsub("\x01→/ol←\x01", "</li></ol>←END\x01").
gsub("</dl>←END\x01<dl>", '').
gsub("</ul>←END\x01<ul>", '').
gsub("</ol>←END\x01<ol>", '').
gsub("←END\x01", '')
end

def headline(level, label, caption)
Expand Down
16 changes: 15 additions & 1 deletion lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,21 @@ def result
if @chapter.is_a?(ReVIEW::Book::Part) && !@book.config.check_version('2', exception: false)
puts '\end{reviewpart}'
end
@output.string
solve_nest(@output.string)
end

def solve_nest(s)
check_nest
s.gsub("\\end{description}\n\n\x01→dl←\x01\n", "\n").
gsub("\x01→/dl←\x01", "\\end{description}←END\x01").
gsub("\\end{itemize}\n\n\x01→ul←\x01\n", "\n").
gsub("\x01→/ul←\x01", "\\end{itemize}←END\x01").
gsub("\\end{enumerate}\n\n\x01→ol←\x01\n", "\n").
gsub("\x01→/ol←\x01", "\\end{enumerate}←END\x01").
gsub("\\end{description}←END\x01\n\n\\begin{description}", '').
gsub("\\end{itemize}←END\x01\n\n\\begin{itemize}", '').
gsub("\\end{enumerate}←END\x01\n\n\\begin{enumerate}", '').
gsub("←END\x01", '')
end

HEADLINE = {
Expand Down
23 changes: 21 additions & 2 deletions lib/review/plaintextbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2019 Kenshi Muto
# Copyright (c) 2018-2020 Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -74,7 +74,26 @@ def blank
private :blank

def result
@output.string
solve_nest(@output.string)
end

def solve_nest(s)
check_nest
lines = []
clevel = []
s.split("\n", -1).each do |l| # -1 means don't omit last "\n"
if l =~ /\A\x01→(dl|ul|ol)←\x01/
clevel.push($1)
lines.push("\x01→END←\x01")
elsif l =~ %r{\A\x01→/(dl|ul|ol)←\x01}
clevel.pop
lines.push("\x01→END←\x01")
else
lines.push("\t" * clevel.size + l)
end
end

lines.join("\n").gsub(/\n*\x01→END←\x01\n*/, "\n")
end

def headline(level, _label, caption)
Expand Down
4 changes: 2 additions & 2 deletions lib/review/rstbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2008-2019 Minero Aoki, Kenshi Muto
# Copyright (c) 2008-2020 Minero Aoki, Kenshi Muto
# 2002-2006 Minero Aoki
#
# This program is free software.
Expand Down Expand Up @@ -86,7 +86,7 @@ def blank
private :blank

def result
@output.string
solve_nest(@output.string)
end

def headline(level, label, caption)
Expand Down
Loading

0 comments on commit ce51f1b

Please sign in to comment.