Skip to content

Commit

Permalink
Merge pull request #1497 from kmuto/childv2
Browse files Browse the repository at this point in the history
//beginchild, //endchildを使った自動判定による箇条書き子要素指示
  • Loading branch information
kmuto authored Sep 28, 2020
2 parents 49bf0db + 02c9a74 commit c8c325e
Show file tree
Hide file tree
Showing 14 changed files with 1,356 additions and 28 deletions.
70 changes: 68 additions & 2 deletions doc/format.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ puts "hello world!"
//}
```


### ソースコード専用の引用

ソースコードを引用するには次のように記述します。
Expand Down Expand Up @@ -925,9 +924,76 @@ LaTeXビルダを使用している場合:
```


`//raw``//embed``@<raw>` および `@<embed>` は、HTML、XML や TeX の文書構造を容易に壊す可能性があります。使用には十分に注意してください。

### 入れ子の箇条書き

Re:VIEW の箇条書きは `*` 型の箇条書きを除き、基本的に入れ子を表現できません。いずれの箇条書きも、別の箇条書き、あるいは図表・リストを箇条書きの途中に配置することを許容していません。

この対策として、Re:VIEW 4.2 では試験的に `//beginchild``//endchild` というブロック命令を追加しています。箇条書きの途中に何かを含めたいときには、それを `//beginchild``//endchild` で囲んで配置します。多重に入れ子にすることも可能です。

```
* UL1
//beginchild
#@# ここからUL1の子
1. UL1-OL1
//beginchild
#@# ここからUL1-OL1の子
UL1-OL1-PARAGRAPH
* UL1-OL1-UL1
* UL1-OL1-UL2
//endchild
#@# ここまでUL1-OL1の子
2. UL1-OL2
: UL1-DL1
UL1-DD1
: UL1-DL2
UL1-DD2
//endchild
#@# ここまでUL1の子
* UL2
```

これをたとえば HTML に変換すると、次のようになります。

```
<ul>
<li>UL1
<ol>
<li>UL1-OL1
<p>UL1-OL1-PARAGRAPH</p>
<ul>
<li>UL1-OL1-UL1</li>
<li>UL1-OL1-UL2</li>
</ul>
</li>
<li>UL1-OL2</li>
</ol>
<dl>
<dt>UL1-DL1</dt>
<dd>UL1-DD1</dd>
<dt>UL1-DL2</dt>
<dd>UL1-DD2</dd>
</dl>
</li>
<li>UL2</li>
</ul>
```

(試験実装のため、命令名や挙動は今後のバージョンで変更になる可能性があります。)

## インライン命令
主なインライン命令を次に示します。

Expand Down
74 changes: 69 additions & 5 deletions doc/format.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ Re:VIEW is based on EWB of ASCII (now KADOKAWA), influenced RD and other Wiki sy

This document explains about the format of Re:VIEW 5.0.


## Paragraph

Paragraphs are separated by an empty line.
Expand Down Expand Up @@ -284,7 +283,6 @@ puts "hello world!"
//}
```


### Quoting Source Code

`//source` is for quoting source code. filename is mandatory.
Expand Down Expand Up @@ -339,7 +337,6 @@ You can use `//image{ ... //}` for figures.
You can write comments or Ascii art in the block as an alternative description.
When publishing, it's simply ignored.


Usage:

```
Expand Down Expand Up @@ -969,6 +966,75 @@ this is a special line.

(In other formats, it is just ignored.)

### Nested itemize block

Re:VIEW itemize blocks basically cannot express nested items. Also, none of itemize blocks allow to contain another itemize block or paragraph/image/table/list.

As a workaround, Re:VIEW 4.2 provides an experimental `//beginchild` and `//endchild`. If you want to include something in an itemize block, enclose it with `//beginchild` and `//endchild`. It is also possible to create a multiple nest.

```
* UL1
//beginchild
#@# child of UL1 start
1. UL1-OL1
//beginchild
#@# child of UL1-OL1 start
UL1-OL1-PARAGRAPH
* UL1-OL1-UL1
* UL1-OL1-UL2
//endchild
#@# child of UL1-OL1 end
2. UL1-OL2
: UL1-DL1
UL1-DD1
: UL1-DL2
UL1-DD2
//endchild
#@# child of UL1 end
* UL2
```

Output:

(In HTML:)

```
<ul>
<li>UL1
<ol>
<li>UL1-OL1
<p>UL1-OL1-PARAGRAPH</p>
<ul>
<li>UL1-OL1-UL1</li>
<li>UL1-OL1-UL2</li>
</ul>
</li>
<li>UL1-OL2</li>
</ol>
<dl>
<dt>UL1-DL1</dt>
<dd>UL1-DD1</dd>
<dt>UL1-DL2</dt>
<dd>UL1-DD2</dd>
</dl>
</li>
<li>UL2</li>
</ul>
```

(This is an experimental implementation. Names and behaviors may change in future versions.)

## Inline Commands

Expand Down Expand Up @@ -1119,13 +1185,11 @@ C
A,B,C
```
## HTML/LaTeX Layout
`layouts/layout.html.erb` and `layouts/layout.tex.erb` are used as layout file.
You can use ERb tags in the layout files.
Sample layout file(layout.html.erb):
```html
Expand Down
33 changes: 31 additions & 2 deletions lib/review/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ def post_paragraph
nil
end

attr_accessor :doc_status
attr_accessor :doc_status, :previous_list_type

def initialize(strict = false, *_args)
@strict = strict
@output = nil
@logger = ReVIEW.logger
@doc_status = {}
@dictionary = {}
@previous_list_type = nil
end

def bind(compiler, chapter, location)
Expand Down Expand Up @@ -90,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 "//beginchild of #{@children.reverse.join(',')} misses //endchild"
end
end

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

alias_method :raw_result, :result
Expand Down Expand Up @@ -713,6 +725,23 @@ def escape(str)
str
end

def beginchild
@children ||= []
unless @previous_list_type
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"
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")
Expand Down
12 changes: 11 additions & 1 deletion lib/review/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def initialize(builder)
@command_name_stack = []
end

attr_reader :builder
attr_reader :builder, :previous_list_type

def strategy
error 'Compiler#strategy is obsoleted. Use Compiler#builder.'
Expand Down Expand Up @@ -191,6 +191,8 @@ def inline_defined?(name)
defsingle :include, 1
defsingle :olnum, 1
defsingle :firstlinenum, 1
defsingle :beginchild, 0
defsingle :endchild, 0

definline :chapref
definline :chap
Expand Down Expand Up @@ -267,15 +269,20 @@ def do_compile
f.gets # Nothing to do
when /\A=+[\[\s{]/
compile_headline(f.gets)
@builder.previous_list_type = nil
when /\A\s+\*/
compile_ulist(f)
@builder.previous_list_type = 'ul'
when /\A\s+\d+\./
compile_olist(f)
@builder.previous_list_type = 'ol'
when /\A\s+:\s/
compile_dlist(f)
@builder.previous_list_type = 'dl'
when /\A\s*:\s/
warn 'Definition list starting with `:` is deprecated. It should start with ` : `.'
compile_dlist(f)
@builder.previous_list_type = 'dl'
when %r{\A//\}}
if in_minicolumn?
_line = f.gets
Expand Down Expand Up @@ -305,19 +312,22 @@ def do_compile
compile_command(syntax, args, lines)
@command_name_stack.pop
end
@builder.previous_list_type = nil
when %r{\A//}
line = f.gets
warn "`//' seen but is not valid command: #{line.strip.inspect}"
if block_open?(line)
warn 'skipping block...'
read_block(f, false)
end
@builder.previous_list_type = nil
else
if f.peek.strip.empty?
f.gets
next
end
compile_paragraph(f)
@builder.previous_list_type = nil
end
end
close_all_tagged_section
Expand Down
16 changes: 15 additions & 1 deletion lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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 @@ -103,6 +103,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
16 changes: 15 additions & 1 deletion lib/review/idgxmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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
Loading

0 comments on commit c8c325e

Please sign in to comment.