Class: AsciidoctorBibtex::Asciidoctor::CitationProcessor
- Inherits:
-
Asciidoctor::Extensions::Treeprocessor
- Object
- Asciidoctor::Extensions::Treeprocessor
- AsciidoctorBibtex::Asciidoctor::CitationProcessor
- Defined in:
- lib/asciidoctor-bibtex/extensions.rb
Overview
CitationProcessor
A tree processor to replace all citations and bibliography.
This processor scans the document, generates a list of citations, replaces each citation with citation text and the reference block macro placeholder with the final bibliography list. It relies on the block macro processor to generate the place holder.
NOTE: According to the asiidoctor extension policy, the tree processor can only produce texts with inline macros.
Instance Method Summary collapse
-
#parse_asciidoc(parent, content, attributes = {}) ⇒ Object
This is an adapted version of Asciidoctor::Extension::parse_content, where resultant blocks are returned as a list instead of attached to the parent.
- #process(document) ⇒ Object
Instance Method Details
#parse_asciidoc(parent, content, attributes = {}) ⇒ Object
This is an adapted version of Asciidoctor::Extension::parse_content, where resultant blocks are returned as a list instead of attached to the parent.
184 185 186 187 188 189 190 191 192 |
# File 'lib/asciidoctor-bibtex/extensions.rb', line 184 def parse_asciidoc(parent, content, attributes = {}) result = [] reader = ::Asciidoctor::Reader.new content while reader.has_more_lines? block = ::Asciidoctor::Parser.next_block reader, parent, attributes result << block if block end result end |
#process(document) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/asciidoctor-bibtex/extensions.rb', line 63 def process(document) bibtex_file = (document.attr 'bibtex-file').to_s bibtex_style = ((document.attr 'bibtex-style') || 'ieee').to_s bibtex_locale = ((document.attr 'bibtex-locale') || 'en-US').to_s bibtex_order = ((document.attr 'bibtex-order') || 'appearance').to_sym bibtex_format = ((document.attr 'bibtex-format') || 'asciidoc').to_sym bibtex_throw = ((document.attr 'bibtex-throw') || 'false').to_s.downcase bibtex_citation_template = ((document.attr 'bibtex-citation-template') || '[$id]').to_s # Fild bibtex file automatically if not supplied. if bibtex_file.empty? bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile document.base_dir end if bibtex_file.empty? bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile '.' end if bibtex_file.empty? bibtex_file = AsciidoctorBibtex::PathUtils.find_bibfile "#{ENV['HOME']}/Documents" end if bibtex_file.empty? puts 'Error: bibtex-file is not set and automatic search failed' exit end # Extract all AST nodes that can contain citations. prose_blocks = document.find_by traverse_documents: true do |b| (b.content_model == :simple) || (b.context == :list_item) || (b.context == :table_cell) || (b.title?) end return nil if prose_blocks.nil? processor = Processor.new bibtex_file, true, bibtex_style, bibtex_locale, bibtex_order == :appearance, bibtex_format, bibtex_throw == 'true', custom_citation_template: bibtex_citation_template # First pass: extract all citations. prose_blocks.each do |block| if block.context == :list_item || block.context == :table_cell # NOTE: we access the instance variable @text for raw text. # Otherwise the footnotes in the text will be pre-processed and # ghost footnotes will be inserted (as of asciidoctor 2.0.10). line = block.instance_variable_get(:@text) unless line.nil? || line.empty? processor.process_citation_macros line end elsif block.content_model == :simple block.lines.each do |line| processor.process_citation_macros line end else line = block.instance_variable_get(:@title) processor.process_citation_macros line end end # Make processor finalize macro processing as required. processor.finalize_macro_processing # Second pass: replace citations with citation texts. prose_blocks.each do |block| if block.context == :list_item || block.context == :table_cell # NOTE: we access the instance variable @text for raw text. line = block.instance_variable_get(:@text) unless line.nil? or line.empty? line_orig = line line = processor.replace_citation_macros(line) line = processor.replace_bibitem_macros(line) block.text = line if line != line_orig end elsif block.content_model == :simple block.lines.each_with_index do |line, index| line_orig = line line = processor.replace_citation_macros(line) line = processor.replace_bibitem_macros(line) block.lines[index] = line if line != line_orig end else # NOTE: we access the instance variable @text for raw text. line = block.instance_variable_get(:@title) line_orig = line line = processor.replace_citation_macros(line) line = processor.replace_bibitem_macros(line) block.title = line if line != line_orig end end references_asciidoc = [] if (bibtex_format == :latex) || (bibtex_format == :bibtex) references_asciidoc << %(+++\\bibliography{#{bibtex_file}}{}+++) references_asciidoc << %(+++\\bibliographystyle{#{bibtex_style}}+++) elsif bibtex_format == :biblatex references_asciidoc << %(+++\\printbibliography+++) else references_asciidoc = processor.build_bibliography_list end # Third pass: replace the bibliography paragraph with the bibliography # list. biblio_blocks = document.find_by traverse_documents: true do |b| # for fast search (since most searches shall fail) (b.content_model == :simple) && (b.lines.size == 1) \ && (b.lines[0] == BibliographyBlockMacroPlaceholder) end biblio_blocks.each do |block| block_index = block.parent.blocks.index do |b| b == block end reference_blocks = parse_asciidoc block.parent, references_asciidoc reference_blocks.reverse.each do |b| block.parent.blocks.insert block_index, b end block.parent.blocks.delete_at block_index + reference_blocks.size end nil end |