Skip to content

Commit

Permalink
add op @<secref>, @<sec>, @<sectitle>. Closes: #1809
Browse files Browse the repository at this point in the history
  • Loading branch information
kmuto committed Apr 9, 2022
1 parent a6c9689 commit f409938
Show file tree
Hide file tree
Showing 13 changed files with 228 additions and 10 deletions.
8 changes: 6 additions & 2 deletions doc/format.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Re:VIEW フォーマットの文法について解説します。Re:VIEW フォーマットはアスキー社(現カドカワ)の EWB を基本としながら、一部に RD や各種 Wiki の文法を取り入れて簡素化しています。

このドキュメントは、Re:VIEW 5.3 に基づいています。
このドキュメントは、Re:VIEW 5.5 に基づいています。

## 段落

Expand Down Expand Up @@ -796,7 +796,7 @@ imgmath_options:
@<hd>{preface|はじめに|まずは}
```

参照先にラベルが設定されている場合は、見出しの代わりに、ラベルで参照します。
参照先にラベルが設定されている場合は、見出しの代わりに、ラベルで参照します。複雑な階層をとるときにはラベルを使うことを推奨します。

```
=={hajimeni} はじめに
Expand All @@ -806,6 +806,10 @@ imgmath_options:
@<hd>{hajimeni|まずは}
```

* `@<hd>{見出しまたはラベル}` あるいは `@<secref>{見出しまたはラベル}` : 『「1.1 まずは」』のように、節項番号とタイトルを含むテキストに置換されます。
* `@<sec>{見出しまたはラベル}` : 「1.1」のような節項番号に置換されます。番号が付かない箇所の場合はエラーになります。
* `@<sectitle>{見出しまたはラベル}` : 「まずは」のように、節項のタイトル部に置換されます。

### コラム見出し参照

コラムの見出しの参照は、インライン命令 `@<column>` を使います。
Expand Down
23 changes: 22 additions & 1 deletion lib/review/builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2002-2021 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2022 Minero Aoki, Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -450,6 +450,27 @@ def inline_hd(id)
app_error "unknown headline: #{id}"
end

alias_method :inline_secref, :inline_hd

def inline_sec(id)
chapter, id = extract_chapter_id(id)
n = chapter.headline_index.number(id)
if n.present? && chapter.number && over_secnolevel?(n)
n
else
app_error "the target headline doesn't have a number: #{id}"
end
rescue KeyError
app_error "unknown headline: #{id}"
end

def inline_sectitle(id)
chapter, id = extract_chapter_id(id)
compile_inline(chapter.headline(id).caption)
rescue KeyError
app_error "unknown headline: #{id}"
end

def inline_column(id)
m = /\A([^|]+)\|(.+)/.match(id)
if m && m[1]
Expand Down
5 changes: 4 additions & 1 deletion lib/review/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2009-2021 Minero Aoki, Kenshi Muto
# Copyright (c) 2009-2022 Minero Aoki, Kenshi Muto
# Copyright (c) 2002-2007 Minero Aoki
#
# This program is free software.
Expand Down Expand Up @@ -232,6 +232,9 @@ def inline_defined?(name)
definline :code
definline :bib
definline :hd
definline :secref
definline :sec
definline :sectitle
definline :href
definline :recipe
definline :column
Expand Down
25 changes: 25 additions & 0 deletions lib/review/htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,31 @@ def inline_hd_chap(chap, id)
app_error "unknown headline: #{id}"
end

def inline_sec(id)
if @book.config['chapterlink']
chap, id2 = extract_chapter_id(id)
n = chap.headline_index.number(id2)
anchor = 'h' + n.tr('.', '-')
%Q(<a href="#{chap.id}#{extname}##{anchor}">#{super(id)}</a>)
else
super(id)
end
rescue KeyError
app_error "unknown headline: #{id}"
end

def inline_sectitle(id)
if @book.config['chapterlink']
chap, id2 = extract_chapter_id(id)
anchor = 'h' + chap.headline_index.number(id2).tr('.', '-')
%Q(<a href="#{chap.id}#{extname}##{anchor}">#{super(id)}</a>)
else
super(id)
end
rescue KeyError
app_error "unknown headline: #{id}"
end

def column_label(id, chapter = @chapter)
num = chapter.column(id).number
"column-#{num}"
Expand Down
14 changes: 13 additions & 1 deletion lib/review/index_builder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2008-2021 Minero Aoki, Kenshi Muto, Masayoshi Takahashi,
# Copyright (c) 2008-2022 Minero Aoki, Kenshi Muto, Masayoshi Takahashi,
# KADO Masanori
#
# This program is free software.
Expand Down Expand Up @@ -517,6 +517,18 @@ def inline_hd(_id)
''
end

def inline_secref(_id)
''
end

def inline_sec(_id)
''
end

def inline_sectitle(_id)
''
end

def inline_bib(_id)
''
end
Expand Down
24 changes: 23 additions & 1 deletion lib/review/latexbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) 2002-2007 Minero Aoki
# 2008-2009 Minero Aoki, Kenshi Muto
# 2010-2021 Minero Aoki, Kenshi Muto, TAKAHASHI Masayoshi
# 2010-2022 Minero Aoki, Kenshi Muto, TAKAHASHI Masayoshi
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down Expand Up @@ -1290,6 +1290,28 @@ def inline_hd_chap(chap, id)
end
end

def inline_sec(id)
if @book.config['chapterlink']
n = super(id)
anchor = n.tr('.', '-')
macro('reviewsecref', n, sec_label(anchor))
else
super(id)
end
rescue KeyError
app_error "unknown headline: #{id}"
end

def inline_sectitle(id)
if @book.config['chapterlink']
chap, id2 = extract_chapter_id(id)
anchor = chap.headline_index.number(id2).tr('.', '-')
macro('reviewsecref', super(id), sec_label(anchor))
else
super(id)
end
end

def inline_column_chap(chapter, id)
macro('reviewcolumnref',
I18n.t('column', compile_inline(chapter.column(id).caption)),
Expand Down
2 changes: 1 addition & 1 deletion lib/review/plaintextbuilder.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2021 Kenshi Muto
# Copyright (c) 2018-2022 Kenshi Muto
#
# This program is free software.
# You can distribute or modify this program under the terms of
Expand Down
8 changes: 7 additions & 1 deletion samples/syntax-book/ch02.re
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,16 @@ a_{m1} & \cdots & a_{mn}
* 章題:@<title>{ch01}、@<title>{part2}、@<title>{appA}、@<title>{bib}
* 章番号+題:@<chapref>{ch02}、@<chapref>{part2}、@<chapref>{appA}、@<chapref>{bib}

節や項への参照はhdを使います
節や項への参照はhdまたはsecを使います

* @<hd>{ch02|ブロック命令}の@<hd>{ch02|ブロック命令|図}
* @<hd>{ch02|crossref}
* @<secref>{ch02|ブロック命令}の@<secref>{ch02|ブロック命令|図}
* @<secref>{ch02|crossref}
* @<sec>{ch02|ブロック命令}の@<sec>{ch02|ブロック命令|図}
* @<sec>{ch02|crossref}
* @<sectitle>{ch02|ブロック命令}の@<sectitle>{ch02|ブロック命令|図}
* @<sectitle>{ch02|crossref}
* コラム参照 @<column>{ch03|column2}

他章への図表リスト参照の例です(@<list>{pre01|main1}、@<img>{pre01|fractal}、@<table>{pre01|tbl1}、@<list>{appA|lista-1}、@<img>{appA|puzzle}、@<table>{appA|taba-1})。
Expand Down
4 changes: 2 additions & 2 deletions test/assets/syntax_book_index_detail.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
-----------------------------
169C 2L 0.2P 第II部 部見出し■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□
=============================
5796C 180L 11P ch02
5858C 186L 11P ch02
-----------------------------
52C 1L 0.0P 第2章 長い章見出し■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□■□
10C 1L 0.0P 2.1 ブロック命令
Expand All @@ -39,7 +39,7 @@
11C 1L 0.0P 2.4 インライン命令
967C 17L 1.3P 2.4.1 書体
74C 1L 0.0P 2.4.2 見出し内 BOLD,ITALIC,TT,STRONG,EM,CODE,TTB,TTI,AMI,BOU,KW,UNDERLINE,INS、
797C 22L 1.3P 2.4.3 参照
859C 28L 1.6P 2.4.3 参照
39C 2L 0.1P 2.4.4 参考文献
194C 3L 0.3P 2.4.5 索引
34C 3L 0.1P 2.4.5.1 後注
Expand Down
34 changes: 34 additions & 0 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,40 @@ def @chapter.headline_index
end
end

def test_inline_sec
def @chapter.headline_index
item = Book::Index::Item.new('chap1|test', [1, 1], 'te_st<>')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end

@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test <a href="-.html#h1-1-1">「1.1.1 te_st&lt;&gt;」</a>', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test <a href="-.html#h1-1-1">te_st&lt;&gt;</a>', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test <a href="-.html#h1-1-1">1.1.1</a>', actual

@config['secnolevel'] = 2
actual = compile_inline('test @<secref>{test}')
assert_equal 'test <a href="-.html#h1-1-1">「te_st&lt;&gt;」</a>', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test <a href="-.html#h1-1-1">te_st&lt;&gt;</a>', actual
assert_raises(ReVIEW::ApplicationError) { compile_block('test @<sec>{test}') }
assert_match(/the target headline doesn't have a number/, @log_io.string)

@config['chapterlink'] = nil
@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test 「1.1.1 te_st&lt;&gt;」', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st&lt;&gt;', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test 1.1.1', actual
end

def test_inline_uchar
actual = compile_inline('test @<uchar>{2460} test2')
assert_equal 'test &#x2460; test2', actual
Expand Down
34 changes: 34 additions & 0 deletions test/test_latexbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,40 @@ def @chapter.headline_index
assert_equal 'test 「1.1.1 te\\textunderscore{}st」 test2', actual
end

def test_inline_sec
def @chapter.headline_index
item = Book::Index::Item.new('chap1|test', [1, 1], 'te_st<>')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end

@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test \reviewsecref{「1.1.1 te\textunderscore{}st\textless{}\textgreater{}」}{sec:1-1-1}', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test \reviewsecref{te\textunderscore{}st\textless{}\textgreater{}}{sec:1-1-1}', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test \reviewsecref{1.1.1}{sec:1-1-1}', actual

@config['secnolevel'] = 2
actual = compile_inline('test @<secref>{test}')
assert_equal 'test \reviewsecref{「te\textunderscore{}st\textless{}\textgreater{}」}{sec:1-1-1}', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test \reviewsecref{te\textunderscore{}st\textless{}\textgreater{}}{sec:1-1-1}', actual
assert_raises(ReVIEW::ApplicationError) { compile_block('test @<sec>{test}') }
assert_match(/the target headline doesn't have a number/, @log_io.string)

@config['chapterlink'] = nil
@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test 「1.1.1 te\textunderscore{}st\textless{}\textgreater{}」', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te\textunderscore{}st\textless{}\textgreater{}', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test 1.1.1', actual
end

def test_inline_pageref
actual = compile_inline('test p.@<pageref>{p1}')
assert_equal 'test p.\pageref{p1}', actual
Expand Down
32 changes: 32 additions & 0 deletions test/test_markdownbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,38 @@ def @chapter.headline_index
assert_equal 'test 「1.1.1 te_st」 test2', actual
end

def test_inline_sec
def @chapter.headline_index
item = Book::Index::Item.new('chap1|test', [1, 1], 'te_st<>')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end

@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test <a href="#h1-1-1">「1.1.1 te_st<>」</a>', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test 1.1.1', actual

@config['secnolevel'] = 2
actual = compile_inline('test @<secref>{test}')
assert_equal 'test <a href="#h1-1-1">「te_st<>」</a>', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
assert_raises(ReVIEW::ApplicationError) { compile_block('test @<sec>{test}') }
@config['chapterlink'] = nil
@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test 「1.1.1 te_st<>」', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test 1.1.1', actual
end

def test_ul_nest1
src = <<-EOS
* AAA
Expand Down
25 changes: 25 additions & 0 deletions test/test_plaintextbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ def test_href_without_label
assert_equal 'http://github.com', actual
end

def test_inline_sec
def @chapter.headline_index
item = Book::Index::Item.new('chap1|test', [1, 1], 'te_st<>')
idx = Book::HeadlineIndex.new(self)
idx.add_item(item)
idx
end

@config['secnolevel'] = 3
actual = compile_inline('test @<secref>{test}')
assert_equal 'test 「1.1.1 te_st<>」', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
actual = compile_inline('test @<sec>{test}')
assert_equal 'test 1.1.1', actual

@config['secnolevel'] = 2
actual = compile_inline('test @<secref>{test}')
assert_equal 'test 「te_st<>」', actual
actual = compile_inline('test @<sectitle>{test}')
assert_equal 'test te_st<>', actual
assert_raises(ReVIEW::ApplicationError) { compile_block('test @<sec>{test}') }
assert_match(/the target headline doesn't have a number/, @log_io.string)
end

def test_inline_raw
actual = compile_inline('@<raw>{@<tt>{inline\}}')
assert_equal '@<tt>{inline}', actual
Expand Down

0 comments on commit f409938

Please sign in to comment.