From 81e622c4aadd67f79849784228b87e39b1585e7f Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 01:21:54 +0900 Subject: [PATCH 01/16] Add bibliography feature --- lib/review/book/bibliography.rb | 52 ++++++++++++++++ lib/review/book/chapter.rb | 16 ++++- lib/review/builder.rb | 8 +++ lib/review/compiler.rb | 2 + lib/review/htmlbuilder.rb | 8 +++ lib/review/latexbuilder.rb | 8 +++ review.gemspec | 3 + test/test_bibliography.rb | 105 ++++++++++++++++++++++++++++++++ 8 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 lib/review/book/bibliography.rb create mode 100644 test/test_bibliography.rb diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb new file mode 100644 index 000000000..e4601db26 --- /dev/null +++ b/lib/review/book/bibliography.rb @@ -0,0 +1,52 @@ +begin + require 'bibtex' + require 'citeproc' + require 'csl/styles' +rescue LoadError + # raise ReVIEW::ConfigError inside the class +end + +module ReVIEW + module Book + class Bibliography + def initialize(bibfile, config = nil) + @bibtex = BibTeX.parse(bibfile, filter: :latex) + @config = config + format('text') + rescue NameError + raise ReVIEW::ConfigError, 'not found bibtex libraries. disabled bibtex feature.' + end + + def format(format) + style = @config['bib-csl-style'] || 'acm-siggraph' + @citeproc = CiteProc::Processor.new(style: style, format: format) + @citeproc.import(@bibtex.to_citeproc) + self + rescue NameError + raise ReVIEW::ConfigError, 'not found bibtex libraries. disabled bibtex feature.' + end + + def ref(key) + cited = @citeproc.render(:citation, id: key) + + # FIXME: need to apply CSL style + if cited == '' + idx = 1 + @citeproc.bibliography.ids.each do |i| + if i == key + cited = "[#{idx}]" + break + end + idx += 1 + end + end + + cited + end + + def list + @citeproc.bibliography.join + end + end + end +end diff --git a/lib/review/book/chapter.rb b/lib/review/book/chapter.rb index c9fc475fd..8f26315c0 100644 --- a/lib/review/book/chapter.rb +++ b/lib/review/book/chapter.rb @@ -10,11 +10,12 @@ require 'review/book/book_unit' require 'review/lineinput' require 'review/preprocessor' +require 'review/book/bibliography' module ReVIEW module Book class Chapter < BookUnit - attr_reader :number, :book + attr_reader :number, :book, :bibliography def self.mkchap(book, name, number = nil) name += book.ext if File.extname(name).empty? @@ -51,6 +52,19 @@ def initialize(book, number, name, path, io = nil) if !@content && @path && File.exist?(@path) @content = File.read(@path, mode: 'rt:BOM|utf-8') @number = nil if %w[nonum nodisp notoc].include?(find_first_header_option) + + # bibliography + if @book && @book.config + chapter_bibfile = File.join(File.dirname(@path), File.basename(@path, '.re') + '.bib') + if File.exist?(chapter_bibfile) + @bibliography = Book::Bibliography.new(chapter_bibfile, @book.config) + else + book_bibfile = File.join(@book.basedir, @book.config['bookname'] + '.bib') + if File.exist?(book_bibfile) + @bibliography = Book::Bibliography.new(book_bibfile, @book.config) + end + end + end end super() diff --git a/lib/review/builder.rb b/lib/review/builder.rb index c39ec9881..09402229e 100644 --- a/lib/review/builder.rb +++ b/lib/review/builder.rb @@ -436,6 +436,14 @@ def bibpaper(lines, id, caption) puts end + def inline_bibref(id) + @chapter.bibliography.format('text').ref(id) + end + + def bibliography + puts @chapter.bibliography.format('text').list + end + def inline_hd(id) m = /\A([^|]+)\|(.+)/.match(id) if m && m[1] diff --git a/lib/review/compiler.rb b/lib/review/compiler.rb index cc2843e45..6e0a02a7e 100644 --- a/lib/review/compiler.rb +++ b/lib/review/compiler.rb @@ -211,6 +211,7 @@ def inline_defined?(name) defsingle :firstlinenum, 1 defsingle :beginchild, 0 defsingle :endchild, 0 + defsingle :bibliography, 0 definline :chapref definline :chap @@ -274,6 +275,7 @@ def inline_defined?(name) definline :pageref definline :w definline :wb + definline :bibref private diff --git a/lib/review/htmlbuilder.rb b/lib/review/htmlbuilder.rb index d61749cf1..4f126df9a 100644 --- a/lib/review/htmlbuilder.rb +++ b/lib/review/htmlbuilder.rb @@ -1095,6 +1095,14 @@ def inline_bib(id) app_error "unknown bib: #{id}" end + def inline_bibref(id) + @chapter.bibliography.format('html').ref(id) + end + + def bibliography + puts @chapter.bibliography.format('html').list + end + def inline_hd_chap(chap, id) n = chap.headline_index.number(id) str = if n.present? && chap.number && over_secnolevel?(n) diff --git a/lib/review/latexbuilder.rb b/lib/review/latexbuilder.rb index 1a7a4d9e5..fa69c5833 100644 --- a/lib/review/latexbuilder.rb +++ b/lib/review/latexbuilder.rb @@ -1275,6 +1275,14 @@ def inline_bib(id) macro('reviewbibref', "[#{@chapter.bibpaper(id).number}]", bib_label(id)) end + def inline_bibref(id) + @chapter.bibliography.format('latex').ref(id) + end + + def bibliography + puts @chapter.bibliography.format('latex').list + end + def inline_hd_chap(chap, id) n = chap.headline_index.number(id) str = if n.present? && chap.number && over_secnolevel?(n) diff --git a/review.gemspec b/review.gemspec index f81be4450..31e5923a0 100644 --- a/review.gemspec +++ b/review.gemspec @@ -35,4 +35,7 @@ Gem::Specification.new do |gem| gem.add_development_dependency('test-unit') gem.add_development_dependency('unicode-eaw') gem.add_development_dependency('webrick') + gem.add_development_dependency('bibtex-ruby') + gem.add_development_dependency('citeproc-ruby') + gem.add_development_dependency('csl-styles') end diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb new file mode 100644 index 000000000..48cba2423 --- /dev/null +++ b/test/test_bibliography.rb @@ -0,0 +1,105 @@ +require 'book_test_helper' +require 'review/book/bibliography' + +class BibliographyTest < Test::Unit::TestCase + include BookTestHelper + def setup + mktmpbookdir do |_dir, book, _files| + @book = book + end + @bib = Book::Bibliography.new(bibfile, @book.config) + end + + def test_new + assert @bib + end + + def test_ref_text + @book.config['bib-csl-style'] = 'acm-siggraph' + assert_equal '[Thomas et al. 2009]', @bib.format('text').ref('pickaxe') + + @book.config['bib-csl-style'] = 'apa' + assert_equal '(Thomas et al., 2009)', @bib.format('text').ref('pickaxe') + + @book.config['bib-csl-style'] = 'ieee' + assert_equal '[1]', @bib.format('text').ref('pickaxe') + end + + def test_cite_html + @book.config['bib-csl-style'] = 'acm-siggraph' + assert_equal '[Thomas et al. 2009]', @bib.format('html').ref('pickaxe') + + @book.config['bib-csl-style'] = 'apa' + assert_equal '(Thomas et al., 2009)', @bib.format('html').ref('pickaxe') + end + + def test_ref_latex + @book.config['bib-csl-style'] = 'acm-siggraph' + assert_equal '[Thomas et al. 2009]', @bib.format('latex').ref('pickaxe') + + @book.config['bib-csl-style'] = 'apa' + assert_equal '(Thomas et al., 2009)', @bib.format('latex').ref('pickaxe') + end + + def test_list + @book.config['bib-csl-style'] = 'acm-siggraph' + expect = <<-EOS +Fraassen, B.C. van. 1989. Laws and Symmetry. Oxford University Press, Oxford. +Thomas, D., Fowler, C., and Hunt, A. 2009. Programming Ruby 1.9: The Pragmatic Programmer’s Guide. The Pragmatic Bookshelf, Raleigh, North Carolina. +EOS + assert_equal expect.chomp, @bib.format('text').list + end + + def test_list_html + @book.config['bib-csl-style'] = 'acm-siggraph' + expect = <<-EOS +
    +
  1. Fraassen, B.C. van. 1989. Laws and Symmetry. Oxford University Press, Oxford.
  2. +
  3. Thomas, D., Fowler, C., and Hunt, A. 2009. Programming Ruby 1.9: The Pragmatic Programmer’s Guide. The Pragmatic Bookshelf, Raleigh, North Carolina.
  4. +
+EOS + assert_equal expect.chomp, @bib.format('html').list + + @book.config['bib-csl-style'] = 'ieee' + expect = <<-EOS +
    +
  1. [1]D. Thomas, C. Fowler, and A. Hunt, Programming Ruby 1.9: The Pragmatic Programmer’s Guide. Raleigh, North Carolina: The Pragmatic Bookshelf, 2009.
  2. +
  3. [2]B. C. van Fraassen, Laws and Symmetry. Oxford: Oxford University Press, 1989.
  4. +
+EOS + assert_equal expect.chomp, @bib.format('html').list + end + + def test_list_latex + @book.config['bib-csl-style'] = 'acm-siggraph' + expect = <<-EOS +\\begin{enumerate} +\\item Fraassen, B.C. van. 1989. \\emph{Laws and Symmetry}. Oxford University Press, Oxford. +\\item Thomas, D., Fowler, C., and Hunt, A. 2009. \\emph{Programming Ruby 1.9: The Pragmatic Programmer’s Guide}. The Pragmatic Bookshelf, Raleigh, North Carolina. +\\end{enumerate} +EOS + assert_equal expect.chomp, @bib.format('latex').list + end + + private + + def bibfile + <<-EOS +@book{pickaxe, + address = {Raleigh, North Carolina}, + author = {Thomas, Dave and Fowler, Chad and Hunt, Andy}, + publisher = {The Pragmatic Bookshelf}, + series = {The Facets of Ruby}, + title = {Programming Ruby 1.9: The Pragmatic Programmer's Guide}, + year = {2009} +} +@book{fraassen_1989, + Address = {Oxford}, + Author = {Bas C. van Fraassen}, + Publisher = {Oxford University Press}, + Title = {Laws and Symmetry}, + Year = 1989 +} +EOS + end +end From a3f30c8a559771e69b2f281ac08523dae0de700f Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 01:31:53 +0900 Subject: [PATCH 02/16] Dependencies should be sorted in an alphabetical order within their section of the gemspec. --- review.gemspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/review.gemspec b/review.gemspec index 31e5923a0..a05e6e6cc 100644 --- a/review.gemspec +++ b/review.gemspec @@ -25,6 +25,9 @@ Gem::Specification.new do |gem| gem.add_dependency('rouge') gem.add_dependency('rubyzip') gem.add_dependency('tty-logger') + gem.add_development_dependency('bibtex-ruby') + gem.add_development_dependency('citeproc-ruby') + gem.add_development_dependency('csl-styles') gem.add_development_dependency('mini_magick') gem.add_development_dependency('pygments.rb') gem.add_development_dependency('rake') @@ -35,7 +38,4 @@ Gem::Specification.new do |gem| gem.add_development_dependency('test-unit') gem.add_development_dependency('unicode-eaw') gem.add_development_dependency('webrick') - gem.add_development_dependency('bibtex-ruby') - gem.add_development_dependency('citeproc-ruby') - gem.add_development_dependency('csl-styles') end From ea2d72eb1cbb783448c5ebfd538dce291524f785 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 02:10:32 +0900 Subject: [PATCH 03/16] Handle multiple references --- lib/review/book/bibliography.rb | 23 ++++++++++++++--------- test/test_bibliography.rb | 17 +++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb index e4601db26..1a65865c3 100644 --- a/lib/review/book/bibliography.rb +++ b/lib/review/book/bibliography.rb @@ -26,19 +26,24 @@ def format(format) raise ReVIEW::ConfigError, 'not found bibtex libraries. disabled bibtex feature.' end - def ref(key) - cited = @citeproc.render(:citation, id: key) + def ref(keys) + authors = [] + keys.split(',').each do |key| + authors << { id: key.strip } + end + + cited = @citeproc.render(:citation, authors) # FIXME: need to apply CSL style - if cited == '' - idx = 1 - @citeproc.bibliography.ids.each do |i| - if i == key - cited = "[#{idx}]" - break + if cited.gsub(/[\s,]/, '') == '' + refnums = [] + refnames = authors.map{ |i| i.values }.flatten + @citeproc.bibliography.ids.each_with_index do |key, idx| + if refnames.include?(key) + refnums << (idx + 1) end - idx += 1 end + cited = "[#{refnums.join(', ')}]" end cited diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index 48cba2423..dbf44254b 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -25,6 +25,23 @@ def test_ref_text assert_equal '[1]', @bib.format('text').ref('pickaxe') end + def test_ref_text_multiple + @book.config['bib-csl-style'] = 'acm-siggraph' + assert_equal '[Thomas et al. 2009; van Fraassen 1989]', + @bib.format('text').ref('pickaxe,fraassen_1989') + + assert_equal '[Thomas et al. 2009; van Fraassen 1989]', + @bib.format('text').ref('pickaxe, fraassen_1989') + + @book.config['bib-csl-style'] = 'apa' + assert_equal '(Thomas et al., 2009; van Fraassen, 1989)', + @bib.format('text').ref('pickaxe,fraassen_1989') + + @book.config['bib-csl-style'] = 'ieee' + assert_equal '[1, 2]', + @bib.format('text').ref('pickaxe,fraassen_1989') + end + def test_cite_html @book.config['bib-csl-style'] = 'acm-siggraph' assert_equal '[Thomas et al. 2009]', @bib.format('html').ref('pickaxe') From b56996d3a6339a70be48a0b88b7d3e454bcb5cef Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 05:34:10 +0900 Subject: [PATCH 04/16] Add third book --- test/test_bibliography.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index dbf44254b..b72385aaa 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -62,6 +62,7 @@ def test_list @book.config['bib-csl-style'] = 'acm-siggraph' expect = <<-EOS Fraassen, B.C. van. 1989. Laws and Symmetry. Oxford University Press, Oxford. +Thomas, D. and Hunt, A. 2019. The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition. The Pragmatic Bookshelf. Thomas, D., Fowler, C., and Hunt, A. 2009. Programming Ruby 1.9: The Pragmatic Programmer’s Guide. The Pragmatic Bookshelf, Raleigh, North Carolina. EOS assert_equal expect.chomp, @bib.format('text').list @@ -72,6 +73,7 @@ def test_list_html expect = <<-EOS
  1. Fraassen, B.C. van. 1989. Laws and Symmetry. Oxford University Press, Oxford.
  2. +
  3. Thomas, D. and Hunt, A. 2019. The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition. The Pragmatic Bookshelf.
  4. Thomas, D., Fowler, C., and Hunt, A. 2009. Programming Ruby 1.9: The Pragmatic Programmer’s Guide. The Pragmatic Bookshelf, Raleigh, North Carolina.
EOS @@ -82,6 +84,7 @@ def test_list_html
  1. [1]D. Thomas, C. Fowler, and A. Hunt, Programming Ruby 1.9: The Pragmatic Programmer’s Guide. Raleigh, North Carolina: The Pragmatic Bookshelf, 2009.
  2. [2]B. C. van Fraassen, Laws and Symmetry. Oxford: Oxford University Press, 1989.
  3. +
  4. [3]D. Thomas and A. Hunt, The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition. The Pragmatic Bookshelf, 2019.
EOS assert_equal expect.chomp, @bib.format('html').list @@ -92,6 +95,7 @@ def test_list_latex expect = <<-EOS \\begin{enumerate} \\item Fraassen, B.C. van. 1989. \\emph{Laws and Symmetry}. Oxford University Press, Oxford. +\\item Thomas, D. and Hunt, A. 2019. \\emph{The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition}. The Pragmatic Bookshelf. \\item Thomas, D., Fowler, C., and Hunt, A. 2009. \\emph{Programming Ruby 1.9: The Pragmatic Programmer’s Guide}. The Pragmatic Bookshelf, Raleigh, North Carolina. \\end{enumerate} EOS @@ -117,6 +121,12 @@ def bibfile Title = {Laws and Symmetry}, Year = 1989 } +@book{pragbook, + author = {Thomas, Dave and Hunt, Andy}, + publisher = {The Pragmatic Bookshelf}, + title = {The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition}, + year = {2019} +} EOS end end From e9d56cd2d891702f968e4cc6fb61b7edf0ad078c Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 05:43:26 +0900 Subject: [PATCH 05/16] Add citeproc format --- lib/review/book/bibliography_format.rb | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 lib/review/book/bibliography_format.rb diff --git a/lib/review/book/bibliography_format.rb b/lib/review/book/bibliography_format.rb new file mode 100644 index 000000000..855c43795 --- /dev/null +++ b/lib/review/book/bibliography_format.rb @@ -0,0 +1,29 @@ +begin + require 'citeproc' + require 'citeproc/ruby' +rescue LoadError + # raise ReVIEW::ConfigError inside the class +end + +# https://github.com/inukshuk/citeproc-ruby +module CiteProc + module Ruby + module Formats + class Html + end + + class Latex + def bibliography(bibliography) + bibliography.header = "\\begin{description}" + bibliography.footer = "\\end{description}" + + bibliography.prefix = "\\item[] " + bibliography.suffix = "" + + bibliography.connector = "\n" + bibliography + end + end + end + end +end From f0b262e4c3907adbcc93be663ef708956d63823b Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 05:44:15 +0900 Subject: [PATCH 06/16] Enable to easily customize --- lib/review/book/bibliography.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb index 1a65865c3..8d884817b 100644 --- a/lib/review/book/bibliography.rb +++ b/lib/review/book/bibliography.rb @@ -2,6 +2,7 @@ require 'bibtex' require 'citeproc' require 'csl/styles' + require 'review/book/bibliography_format' rescue LoadError # raise ReVIEW::ConfigError inside the class end @@ -50,7 +51,16 @@ def ref(keys) end def list - @citeproc.bibliography.join + b = @citeproc.bibliography + content = [] + b.references.each_with_index do |r, idx| + content << [b.prefix, r, b.suffix].compact.join + end + [ + b.header, + content.join(b.connector), + b.footer + ].compact.join(b.connector) end end end From 4d1cb0b67a3cf2c279508ac61cc1131e3e5adadf Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 05:44:37 +0900 Subject: [PATCH 07/16] Fix latex bib format --- test/test_bibliography.rb | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index b72385aaa..9c3b7cb32 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -93,11 +93,21 @@ def test_list_html def test_list_latex @book.config['bib-csl-style'] = 'acm-siggraph' expect = <<-EOS -\\begin{enumerate} -\\item Fraassen, B.C. van. 1989. \\emph{Laws and Symmetry}. Oxford University Press, Oxford. -\\item Thomas, D. and Hunt, A. 2019. \\emph{The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition}. The Pragmatic Bookshelf. -\\item Thomas, D., Fowler, C., and Hunt, A. 2009. \\emph{Programming Ruby 1.9: The Pragmatic Programmer’s Guide}. The Pragmatic Bookshelf, Raleigh, North Carolina. -\\end{enumerate} +\\begin{description} +\\item[] Fraassen, B.C. van. 1989. \\emph{Laws and Symmetry}. Oxford University Press, Oxford. +\\item[] Thomas, D. and Hunt, A. 2019. \\emph{The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition}. The Pragmatic Bookshelf. +\\item[] Thomas, D., Fowler, C., and Hunt, A. 2009. \\emph{Programming Ruby 1.9: The Pragmatic Programmer’s Guide}. The Pragmatic Bookshelf, Raleigh, North Carolina. +\\end{description} +EOS + assert_equal expect.chomp, @bib.format('latex').list + + @book.config['bib-csl-style'] = 'ieee' + expect = <<-EOS +\\begin{description} +\\item[] [1]D. Thomas, C. Fowler, and A. Hunt, \\emph{Programming Ruby 1.9: The Pragmatic Programmer’s Guide}. Raleigh, North Carolina: The Pragmatic Bookshelf, 2009. +\\item[] [2]B. C. van Fraassen, \\emph{Laws and Symmetry}. Oxford: Oxford University Press, 1989. +\\item[] [3]D. Thomas and A. Hunt, \\emph{The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition}. The Pragmatic Bookshelf, 2019. +\\end{description} EOS assert_equal expect.chomp, @bib.format('latex').list end From 9223045bf0df0f7474324ea48eb2884c3ccf6e09 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 06:19:01 +0900 Subject: [PATCH 08/16] Handle sist02 --- lib/review/book/bibliography.rb | 2 +- test/test_bibliography.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb index 8d884817b..e4097f067 100644 --- a/lib/review/book/bibliography.rb +++ b/lib/review/book/bibliography.rb @@ -36,7 +36,7 @@ def ref(keys) cited = @citeproc.render(:citation, authors) # FIXME: need to apply CSL style - if cited.gsub(/[\s,]/, '') == '' + if cited.gsub(/[\[\]\(\)\s,]/, '') == '' refnums = [] refnames = authors.map{ |i| i.values }.flatten @citeproc.bibliography.ids.each_with_index do |key, idx| diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index 9c3b7cb32..e3411c35a 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -40,6 +40,10 @@ def test_ref_text_multiple @book.config['bib-csl-style'] = 'ieee' assert_equal '[1, 2]', @bib.format('text').ref('pickaxe,fraassen_1989') + + @book.config['bib-csl-style'] = 'sist02' + assert_equal '[2, 3]', + @bib.format('text').ref('pickaxe,fraassen_1989') end def test_cite_html From 67c279a83a85b8ec8c10bf081c42498f6d5d517a Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 13:23:12 +0900 Subject: [PATCH 09/16] Rubocop --- lib/review/book/bibliography.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb index e4097f067..75cfbc8c6 100644 --- a/lib/review/book/bibliography.rb +++ b/lib/review/book/bibliography.rb @@ -36,9 +36,9 @@ def ref(keys) cited = @citeproc.render(:citation, authors) # FIXME: need to apply CSL style - if cited.gsub(/[\[\]\(\)\s,]/, '') == '' + if cited.gsub(/[\[\]()\s,]/, '') == '' refnums = [] - refnames = authors.map{ |i| i.values }.flatten + refnames = authors.map(&:values).flatten @citeproc.bibliography.ids.each_with_index do |key, idx| if refnames.include?(key) refnums << (idx + 1) @@ -53,7 +53,7 @@ def ref(keys) def list b = @citeproc.bibliography content = [] - b.references.each_with_index do |r, idx| + b.references.each_with_index do |r, _idx| content << [b.prefix, r, b.suffix].compact.join end [ From 433b22fce2dcef9f997437a44b1721175f176e3b Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 13:31:33 +0900 Subject: [PATCH 10/16] Rubocop --- lib/review/book/bibliography_format.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/review/book/bibliography_format.rb b/lib/review/book/bibliography_format.rb index 855c43795..68d991969 100644 --- a/lib/review/book/bibliography_format.rb +++ b/lib/review/book/bibliography_format.rb @@ -9,16 +9,16 @@ module CiteProc module Ruby module Formats - class Html - end + # class Html + # end class Latex def bibliography(bibliography) - bibliography.header = "\\begin{description}" - bibliography.footer = "\\end{description}" + bibliography.header = '\\begin{description}' + bibliography.footer = '\\end{description}' - bibliography.prefix = "\\item[] " - bibliography.suffix = "" + bibliography.prefix = '\\item[] ' + bibliography.suffix = '' bibliography.connector = "\n" bibliography From f088991902ed172971d9a281f7190d297797d559 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 13:31:49 +0900 Subject: [PATCH 11/16] Add test for sist02 --- test/test_bibliography.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index e3411c35a..359c62d5f 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -44,6 +44,8 @@ def test_ref_text_multiple @book.config['bib-csl-style'] = 'sist02' assert_equal '[2, 3]', @bib.format('text').ref('pickaxe,fraassen_1989') + assert_equal '[2, 3]', + @bib.format('text').ref('fraassen_1989,pickaxe') end def test_cite_html @@ -112,6 +114,16 @@ def test_list_latex \\item[] [2]B. C. van Fraassen, \\emph{Laws and Symmetry}. Oxford: Oxford University Press, 1989. \\item[] [3]D. Thomas and A. Hunt, \\emph{The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition}. The Pragmatic Bookshelf, 2019. \\end{description} +EOS + assert_equal expect.chomp, @bib.format('latex').list + + @book.config['bib-csl-style'] = 'sist02' + expect = <<-EOS +\\begin{description} +\\item[] (1)Thomas, Dave, Hunt, Andy. The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition. The Pragmatic Bookshelf, 2019. +\\item[] (2)Fraassen, Bas C. van. Laws and Symmetry. Oxford, Oxford University Press, 1989. +\\item[] (3)Thomas, Dave, Fowler, Chad, Hunt, Andy. Programming Ruby 1.9: The Pragmatic Programmer’s Guide. Raleigh, North Carolina, The Pragmatic Bookshelf, 2009, (The Facets of Ruby). +\\end{description} EOS assert_equal expect.chomp, @bib.format('latex').list end From 42401c392999672f2a2b84517c1792b0708601a5 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Fri, 14 Oct 2022 14:04:18 +0900 Subject: [PATCH 12/16] Set locale to maker sure --- lib/review/book/bibliography.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb index 75cfbc8c6..ed5941fd4 100644 --- a/lib/review/book/bibliography.rb +++ b/lib/review/book/bibliography.rb @@ -20,7 +20,7 @@ def initialize(bibfile, config = nil) def format(format) style = @config['bib-csl-style'] || 'acm-siggraph' - @citeproc = CiteProc::Processor.new(style: style, format: format) + @citeproc = CiteProc::Processor.new(style: style, format: format, locale: 'en-US') @citeproc.import(@bibtex.to_citeproc) self rescue NameError From 3b2205e862161952c52c08652fb64607a07e81b3 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Sun, 16 Oct 2022 19:28:38 +0900 Subject: [PATCH 13/16] Workdaround for SIST02 --- lib/review/book/bibliography.rb | 12 +++++++++--- test/test_bibliography.rb | 22 ++++++++-------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb index ed5941fd4..f3de2f853 100644 --- a/lib/review/book/bibliography.rb +++ b/lib/review/book/bibliography.rb @@ -50,11 +50,17 @@ def ref(keys) cited end - def list + def list(key = nil) b = @citeproc.bibliography content = [] - b.references.each_with_index do |r, _idx| - content << [b.prefix, r, b.suffix].compact.join + + (0..(b.references.size-1)).each do |i| + id = b.ids[i] + reference = b.references[i] + + if key.blank? || key == id + content << [b.prefix, reference, b.suffix].compact.join + end end [ b.header, diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index 359c62d5f..b9d611a6b 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -40,12 +40,6 @@ def test_ref_text_multiple @book.config['bib-csl-style'] = 'ieee' assert_equal '[1, 2]', @bib.format('text').ref('pickaxe,fraassen_1989') - - @book.config['bib-csl-style'] = 'sist02' - assert_equal '[2, 3]', - @bib.format('text').ref('pickaxe,fraassen_1989') - assert_equal '[2, 3]', - @bib.format('text').ref('fraassen_1989,pickaxe') end def test_cite_html @@ -116,16 +110,16 @@ def test_list_latex \\end{description} EOS assert_equal expect.chomp, @bib.format('latex').list + end + + def test_sist02 @book.config['bib-csl-style'] = 'sist02' - expect = <<-EOS -\\begin{description} -\\item[] (1)Thomas, Dave, Hunt, Andy. The Pragmatic Programmer: Your Journey to Mastery, 20th Anniversary Edition. The Pragmatic Bookshelf, 2019. -\\item[] (2)Fraassen, Bas C. van. Laws and Symmetry. Oxford, Oxford University Press, 1989. -\\item[] (3)Thomas, Dave, Fowler, Chad, Hunt, Andy. Programming Ruby 1.9: The Pragmatic Programmer’s Guide. Raleigh, North Carolina, The Pragmatic Bookshelf, 2009, (The Facets of Ruby). -\\end{description} -EOS - assert_equal expect.chomp, @bib.format('latex').list + key = "pickaxe" + + # The sort order depends on the execution environment (OS). + # Therefore if the reference number is the same, it is assumed to be passed. + assert_equal @bib.format('text').list(key)[1], @bib.format('text').ref(key)[1] end private From 101e6f4349fae550d8e58b31011ecd12e497f688 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Sun, 16 Oct 2022 19:30:55 +0900 Subject: [PATCH 14/16] rubocop --- test/test_bibliography.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index b9d611a6b..3f20e6e93 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -115,7 +115,7 @@ def test_list_latex def test_sist02 @book.config['bib-csl-style'] = 'sist02' - key = "pickaxe" + key = 'pickaxe' # The sort order depends on the execution environment (OS). # Therefore if the reference number is the same, it is assumed to be passed. From ad8d91803a5f52af3ac58e6fc2c0a512a1ed64b7 Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Sun, 16 Oct 2022 19:36:38 +0900 Subject: [PATCH 15/16] rubocop --- test/test_bibliography.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_bibliography.rb b/test/test_bibliography.rb index 3f20e6e93..39ccee96a 100644 --- a/test/test_bibliography.rb +++ b/test/test_bibliography.rb @@ -112,7 +112,6 @@ def test_list_latex assert_equal expect.chomp, @bib.format('latex').list end - def test_sist02 @book.config['bib-csl-style'] = 'sist02' key = 'pickaxe' From 89dca98ab37ada191834148704c550c1dd3b4b9e Mon Sep 17 00:00:00 2001 From: Masanori Kado Date: Sun, 16 Oct 2022 19:36:41 +0900 Subject: [PATCH 16/16] rubocop --- lib/review/book/bibliography.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/review/book/bibliography.rb b/lib/review/book/bibliography.rb index f3de2f853..d0f3e139a 100644 --- a/lib/review/book/bibliography.rb +++ b/lib/review/book/bibliography.rb @@ -54,7 +54,7 @@ def list(key = nil) b = @citeproc.bibliography content = [] - (0..(b.references.size-1)).each do |i| + (0..(b.references.size - 1)).each do |i| id = b.ids[i] reference = b.references[i]