diff --git a/.github/workflows/rspec.yml b/.github/workflows/rspec.yml index 7b2668d..5ee07da 100644 --- a/.github/workflows/rspec.yml +++ b/.github/workflows/rspec.yml @@ -5,8 +5,7 @@ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby -name: Ruby - +name: RSpec on: push: branches: [ "master" ] diff --git a/README.org b/README.org index 71aabc9..5b41f79 100644 --- a/README.org +++ b/README.org @@ -9,7 +9,7 @@ An [[http://orgmode.org][Org-mode]] parser written in Ruby. ** Demo See the README file in [[https://ricardo-richo.gitlab.io/org-ruby][Gitlab]] ([[https://ricardo-richo.gitlab.io/org-ruby/]]) or in -[[https://ricardoricho.github.io/org-ruby/][GitHub]] (https://ricardoricho.github.io/org-ruby/) +[[https://ricardoricho.github.io/org-ruby/][GitHub]] ([[https://ricardoricho.github.io/org-ruby/]]) ** Installation diff --git a/lib/org-ruby.rb b/lib/org-ruby.rb index a57777a..39e5167 100644 --- a/lib/org-ruby.rb +++ b/lib/org-ruby.rb @@ -1,5 +1,6 @@ # internal requires require 'org-ruby/version' +require 'orgmode/elements/document' require 'org-ruby/parser' require 'org-ruby/regexp_helper' require 'org-ruby/line' diff --git a/lib/org-ruby/html_output_buffer.rb b/lib/org-ruby/html_output_buffer.rb index 249e1d1..5e5d595 100644 --- a/lib/org-ruby/html_output_buffer.rb +++ b/lib/org-ruby/html_output_buffer.rb @@ -26,11 +26,10 @@ class HtmlOutputBuffer < OutputBuffer attr_reader :options - def initialize(output, opts = {}) - super(output) + def initialize(output, document = nil, opts = {}) + super(output, document) @options = opts @new_paragraph = :start - @footnotes = {} @unclosed_tags = [] # move from output_buffer @@ -218,7 +217,7 @@ def add_headline_id(line) end def html_buffer_code_block_indent(line) - if mode_is_code?(current_mode) && !(line.block_type) + if mode_is_code?(current_mode) && !line.block_type # Determines the amount of whitespaces to be stripped at the # beginning of each line in code block. if line.paragraph_type != :blank @@ -238,20 +237,19 @@ def html_buffer_code_block_indent(line) # defined separately from their references will be rendered where they appear in the original # Org document. def output_footnotes! - return false if !options[:export_footnotes] || @footnotes.empty? + return if !options[:export_footnotes] || document.footnotes.empty? @output.concat footnotes_header - @footnotes.each do |name, (defi, content)| - @buffer = defi - @output << "
#{name}" \ - << "

" \ + document.footnotes.each do |footnote| + @buffer = footnote[:content].empty? && footnote[:label] || footnote[:content] + a_href = footnote[:index] + @output << "

#{a_href}" \ + << "

" \ << inline_formatting(@buffer) \ - << "

\n" + << "

\n" end @output.concat "\n" - - true end def footnotes_header @@ -390,16 +388,24 @@ def inline_formatting(str) str.gsub!(/\s*\|\s*/, quote_tags("")) end - if @options[:export_footnotes] then - @re_help.rewrite_footnote_definition str do |name, content| - quote_tags("") + - name + quote_tags(" ") + content + if @options[:export_footnotes] + @re_help.capture_footnote_definition(str) do |label, content| + # Capture definition and replace it with nil + nil end - @re_help.rewrite_footnote str do |name, defi| - @footnotes[name] = defi if defi - quote_tags("") + - name + quote_tags("") + # Reference footnote + @re_help.rewrite_footnote str do |label, content| + footnote = document.footnotes.find do |footnote| + footnote[:label] == label || footnote[:content] == content + end + + a_id = (footnote[:label].nil? || footnote[:label].empty?) ? footnote[:index] : footnote[:label] + a_text = footnote[:index] + a_href = (footnote[:label].nil? || footnote[:label].empty?) ? footnote[:index] : footnote[:label] + + footnote_tag = "#{a_text}" + quote_tags(footnote_tag) end end diff --git a/lib/org-ruby/line.rb b/lib/org-ruby/line.rb index 4f39d8f..ef15712 100644 --- a/lib/org-ruby/line.rb +++ b/lib/org-ruby/line.rb @@ -62,7 +62,12 @@ def comment? # Determines if a line is an orgmode "headline": # A headline begins with one or more asterisks. def headline? - RegexpHelper.headline.match(to_s) + RegexpHelper.headline.match(@line) + end + + def footnote? + RegexpHelper.footnote_definition.match(@line) || + RegexpHelper.footnote_reference.match(@line) end def property_drawer_begin_block? diff --git a/lib/org-ruby/line_regexp.rb b/lib/org-ruby/line_regexp.rb index b296583..a69b7d2 100644 --- a/lib/org-ruby/line_regexp.rb +++ b/lib/org-ruby/line_regexp.rb @@ -22,6 +22,14 @@ def drawer /^\s*:(?[\w\-]+):$/ end + def footnote_definition + /^\[fn:(?