Skip to content

Commit

Permalink
Add footnotes_title as a parser option (Fix #2).
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoricho committed Nov 21, 2022
1 parent 4c5bffb commit 1f72e6b
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 46 deletions.
72 changes: 52 additions & 20 deletions lib/org-ruby/html_output_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ def initialize(output, opts = {})
@new_paragraph = :start
@footnotes = {}
@unclosed_tags = []
@custom_blocktags = {} if @options[:markup_file]
do_custom_markup if @options[:markup_file]
# move from output_buffer
@code_block_indent = nil

do_custom_markup
end

def buffer_tag
Expand Down Expand Up @@ -101,11 +103,11 @@ def skip_css?(mode)
# entering this mode.
def pop_mode(mode = nil)
m = super(mode)
return @list_indent_stack.pop unless html_tags.include?(m)
return @list_indent_stack.pop if skip_css?(m)
return list_indent_stack.pop unless html_tags.include?(m)
return list_indent_stack.pop if skip_css?(m)
push_indentation(@new_paragraph)
@output.concat("</#{HtmlBlockTag[m]}>")
@list_indent_stack.pop
list_indent_stack.pop
end

def highlight(code, lang)
Expand Down Expand Up @@ -144,7 +146,7 @@ def flush!
else
@output << inline_formatting(definition)
end
indent = @list_indent_stack.last
indent = list_indent_stack.last
pop_mode

@new_paragraph = :start
Expand Down Expand Up @@ -199,13 +201,15 @@ def add_line_attributes headline
end
end

# Only footnotes defined in the footnote (i.e., [fn:0:this is the footnote definition])
# will be automatically
# added to a separate Footnotes section at the end of the document. All footnotes that are
# defined separately from their references will be rendered where they appear in the original
# Org document.
def output_footnotes!
# Only footnotes defined in the footnote (i.e., [fn:0:this is the footnote definition]) will be automatically
# added to a separate Footnotes section at the end of the document. All footnotes that are defined separately
# from their references will be rendered where they appear in the original Org document.
return false unless @options[:export_footnotes] and not @footnotes.empty?
return false if !options[:export_footnotes] || @footnotes.empty?
@output.concat footnotes_header

@output << "\n<div id=\"footnotes\">\n<h2 class=\"footnotes\">Footnotes:</h2>\n<div id=\"text-footnotes\">\n"
@footnotes.each do |name, (defi, content)|
@buffer = defi
@output << "<div class=\"footdef\"><sup><a id=\"fn.#{name}\" href=\"#fnr.#{name}\">#{name}</a></sup>" \
Expand All @@ -219,11 +223,40 @@ def output_footnotes!
return true
end

def footnotes_header
footnotes_title = options[:footnotes_title] || "Footnotes:"
"\n<div id=\"footnotes\">\n<h2 class=\"footnotes\">#{footnotes_title}</h2>\n<div id=\"text-footnotes\">\n"
end

# Test if we're in an output mode in which whitespace is significant.
def preserve_whitespace?
super or current_mode == :html
super || current_mode == :html
end

protected

def do_custom_markup
file = options[:markup_file]
return unless file
return no_custom_markup_file_exists unless File.exists?(file)

@custom_blocktags = load_custom_markup(file)
@custom_blocktags.empty? && no_valid_markup_found ||
set_custom_markup
end

def load_custom_markup(file)
require 'yaml'
if (self.class.to_s == 'Orgmode::MarkdownOutputBuffer')
filter = '^MarkdownMap$'
else
filter = '^HtmlBlockTag$|^Tags$'
end
@custom_blocktags = YAML.load_file(@options[:markup_file]).select {|k| k.to_s.match(filter) }
end



######################################################################
private

Expand Down Expand Up @@ -274,12 +307,12 @@ def quote_tags str
end

def buffer_indentation
" " * @list_indent_stack.length
" " * list_indent_stack.length
end

def add_paragraph
indent = " " * (@list_indent_stack.length - 1)
@output << "\n" << indent
indent = " " * (list_indent_stack.length - 1)
@output.concat "\n#{indent}"
end

Tags = {
Expand All @@ -294,13 +327,12 @@ def add_paragraph

# Applies inline formatting rules to a string.
def inline_formatting(str)
@re_help.rewrite_emphasis(str) do |marker, s|

@re_help.rewrite_emphasis(str) do |marker, text|
if marker == "=" || marker == "~"
s = escapeHTML(s)
"<#{Tags[marker][:open]}>#{s}</#{Tags[marker][:close]}>"
escaped_text = escapeHTML(text)
"<#{Tags[marker][:open]}>#{escaped_text}</#{Tags[marker][:close]}>"
else
quote_tags("<#{Tags[marker][:open]}>") + s +
quote_tags("<#{Tags[marker][:open]}>") + text +
quote_tags("</#{Tags[marker][:close]}>")
end
end
Expand Down
24 changes: 12 additions & 12 deletions lib/org-ruby/output_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Orgmode
class OutputBuffer

# This is the overall output buffer
attr_reader :output, :mode_stack
attr_reader :output, :mode_stack, :list_indent_stack

# This is the current type of output being accumulated.
attr_accessor :output_type, :headline_number_stack
Expand Down Expand Up @@ -81,36 +81,36 @@ def line_get_content(line)
case
when line.assigned_paragraph_type == :comment
# Don't add to buffer
return ""
""
when line.title?
return line.output_text
line.output_text
when line.raw_text?
# This case is for html buffer, because buffer_tag is a method
if line.raw_text_tag == buffer_tag
return "\n#{line.output_text}"
"\n#{line.output_text}"
else
return ""
""
end
when preserve_whitespace?
if line.block_type
return ""
""
else
return "\n#{line.output_text}"
"\n#{line.output_text}"
end
when line.assigned_paragraph_type == :code
# If the line is contained within a code block but we should
# not preserve whitespaces, then we do nothing.
return ""
when (line.kind_of? Headline)
""
when line.is_a?(Headline)
add_line_attributes(line)
return "\n#{line.output_text.strip}"
"\n#{line.output_text.strip}"

when ([:definition_term, :list_item, :table_row, :table_header,
:horizontal_rule].include? line.paragraph_type)

return "\n#{line.output_text.strip}"
"\n#{line.output_text.strip}"
when line.paragraph_type == :paragraph
return "\n""#{buffer_indentation}#{line.output_text.strip}"
"\n""#{buffer_indentation}#{line.output_text.strip}"
else ""
end
end
Expand Down
23 changes: 12 additions & 11 deletions lib/org-ruby/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def initialize(lines, parser_options={ })
@parser_options = parser_options

#
# Include file feature disabled by default since
# Include file feature disabled by default since
# it would be dangerous in some environments
#
# http://orgmode.org/manual/Include-files.html
Expand Down Expand Up @@ -339,14 +339,15 @@ def to_markdown
def to_html
mark_trees_for_export
export_options = {
:decorate_title => @in_buffer_settings["TITLE"],
:export_heading_number => export_heading_number?,
:export_todo => export_todo?,
:use_sub_superscripts => use_sub_superscripts?,
:export_footnotes => export_footnotes?,
:link_abbrevs => @link_abbrevs,
:skip_syntax_highlight => @parser_options[:skip_syntax_highlight],
:markup_file => @parser_options[:markup_file]
decorate_title: in_buffer_settings['TITLE'],
export_heading_number: export_heading_number?,
export_todo: export_todo?,
use_sub_superscripts: use_sub_superscripts?,
export_footnotes: export_footnotes?,
link_abbrevs: @link_abbrevs,
skip_syntax_highlight: @parser_options[:skip_syntax_highlight],
markup_file: @parser_options[:markup_file],
footnotes_title: @parser_options[:footnotes_title]
}
export_options[:skip_tables] = true if not export_tables?
output = ""
Expand Down Expand Up @@ -377,8 +378,8 @@ def to_html
output << "\n"

return output if @parser_options[:skip_rubypants_pass]
rp = RubyPants.new(output)

rp = RubyPants.new(output)
rp.to_html
end

Expand Down
2 changes: 1 addition & 1 deletion lib/org-ruby/regexp_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def match_all(str)
# replace "*bold*", "/italic/", and "=code=",
# respectively. (Clearly this sample string will use HTML-like
# syntax, assuming +map+ is defined appropriately.)
def rewrite_emphasis str
def rewrite_emphasis(str)
# escape the percent signs for safe restoring code snippets
str.gsub!(/%/, "%%")
format_str = "%s"
Expand Down
111 changes: 111 additions & 0 deletions spec/html_examples/footnotes_title.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<h1 class="title">Footnotes</h1>
<p>Using letters and not defined in the footnote <sup><a id="fnr.abc" class="footref" href="#fn.abc">abc</a></sup></p>
<p>Defined in the footnote itself with markup <sup><a id="fnr.0" class="footref" href="#fn.0">0</a></sup></p>
<p>Some example paragraph <sup><a id="fnr.1" class="footref" href="#fn.1">1</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.2" class="footref" href="#fn.2">2</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.3" class="footref" href="#fn.3">3</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.4" class="footref" href="#fn.4">4</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.5" class="footref" href="#fn.5">5</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.6" class="footref" href="#fn.6">6</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.7" class="footref" href="#fn.7">7</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.8" class="footref" href="#fn.8">8</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.9" class="footref" href="#fn.9">9</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.10" class="footref" href="#fn.10">10</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.11" class="footref" href="#fn.11">11</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.12" class="footref" href="#fn.12">12</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.13" class="footref" href="#fn.13">13</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.14" class="footref" href="#fn.14">14</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.15" class="footref" href="#fn.15">15</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.16" class="footref" href="#fn.16">16</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.17" class="footref" href="#fn.17">17</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.18" class="footref" href="#fn.18">18</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.19" class="footref" href="#fn.19">19</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.20" class="footref" href="#fn.20">20</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.21" class="footref" href="#fn.21">21</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.22" class="footref" href="#fn.22">22</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.23" class="footref" href="#fn.23">23</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.24" class="footref" href="#fn.24">24</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.25" class="footref" href="#fn.25">25</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.26" class="footref" href="#fn.26">26</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.27" class="footref" href="#fn.27">27</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.28" class="footref" href="#fn.28">28</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.29" class="footref" href="#fn.29">29</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.30" class="footref" href="#fn.30">30</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.31" class="footref" href="#fn.31">31</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.32" class="footref" href="#fn.32">32</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.33" class="footref" href="#fn.33">33</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.34" class="footref" href="#fn.34">34</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.35" class="footref" href="#fn.35">35</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.36" class="footref" href="#fn.36">36</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.37" class="footref" href="#fn.37">37</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.38" class="footref" href="#fn.38">38</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.39" class="footref" href="#fn.39">39</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.40" class="footref" href="#fn.40">40</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.41" class="footref" href="#fn.41">41</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.42" class="footref" href="#fn.42">42</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.43" class="footref" href="#fn.43">43</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.44" class="footref" href="#fn.44">44</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.45" class="footref" href="#fn.45">45</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.46" class="footref" href="#fn.46">46</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.47" class="footref" href="#fn.47">47</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.48" class="footref" href="#fn.48">48</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.49" class="footref" href="#fn.49">49</a></sup>, with text at the end.</p>
<p>Some example paragraph <sup><a id="fnr.50" class="footref" href="#fn.50">50</a></sup>, with text at the end.</p>
<p><sup><a id="fn.1" class="footnum" href="#fnr.1">1</a></sup> And some example footnote</p>
<p><sup><a id="fn.2" class="footnum" href="#fnr.2">2</a></sup> And some example footnote</p>
<p><sup><a id="fn.3" class="footnum" href="#fnr.3">3</a></sup> And some example footnote</p>
<p><sup><a id="fn.4" class="footnum" href="#fnr.4">4</a></sup> And some example footnote</p>
<p><sup><a id="fn.5" class="footnum" href="#fnr.5">5</a></sup> And some example footnote</p>
<p><sup><a id="fn.6" class="footnum" href="#fnr.6">6</a></sup> And some example footnote</p>
<p><sup><a id="fn.7" class="footnum" href="#fnr.7">7</a></sup> And some example footnote</p>
<p><sup><a id="fn.8" class="footnum" href="#fnr.8">8</a></sup> And some example footnote</p>
<p><sup><a id="fn.9" class="footnum" href="#fnr.9">9</a></sup> And some example footnote</p>
<p><sup><a id="fn.10" class="footnum" href="#fnr.10">10</a></sup> And some example footnote</p>
<p><sup><a id="fn.11" class="footnum" href="#fnr.11">11</a></sup> And some example footnote</p>
<p><sup><a id="fn.12" class="footnum" href="#fnr.12">12</a></sup> And some example footnote</p>
<p><sup><a id="fn.13" class="footnum" href="#fnr.13">13</a></sup> And some example footnote</p>
<p><sup><a id="fn.14" class="footnum" href="#fnr.14">14</a></sup> And some example footnote</p>
<p><sup><a id="fn.15" class="footnum" href="#fnr.15">15</a></sup> And some example footnote</p>
<p><sup><a id="fn.16" class="footnum" href="#fnr.16">16</a></sup> And some example footnote</p>
<p><sup><a id="fn.17" class="footnum" href="#fnr.17">17</a></sup> And some example footnote</p>
<p><sup><a id="fn.18" class="footnum" href="#fnr.18">18</a></sup> And some example footnote</p>
<p><sup><a id="fn.19" class="footnum" href="#fnr.19">19</a></sup> And some example footnote</p>
<p><sup><a id="fn.20" class="footnum" href="#fnr.20">20</a></sup> And some example footnote</p>
<p><sup><a id="fn.21" class="footnum" href="#fnr.21">21</a></sup> And some example footnote</p>
<p><sup><a id="fn.22" class="footnum" href="#fnr.22">22</a></sup> And some example footnote</p>
<p><sup><a id="fn.23" class="footnum" href="#fnr.23">23</a></sup> And some example footnote</p>
<p><sup><a id="fn.24" class="footnum" href="#fnr.24">24</a></sup> And some example footnote</p>
<p><sup><a id="fn.25" class="footnum" href="#fnr.25">25</a></sup> And some example footnote</p>
<p><sup><a id="fn.26" class="footnum" href="#fnr.26">26</a></sup> And some example footnote</p>
<p><sup><a id="fn.27" class="footnum" href="#fnr.27">27</a></sup> And some example footnote</p>
<p><sup><a id="fn.28" class="footnum" href="#fnr.28">28</a></sup> And some example footnote</p>
<p><sup><a id="fn.29" class="footnum" href="#fnr.29">29</a></sup> And some example footnote</p>
<p><sup><a id="fn.30" class="footnum" href="#fnr.30">30</a></sup> And some example footnote</p>
<p><sup><a id="fn.31" class="footnum" href="#fnr.31">31</a></sup> And some example footnote</p>
<p><sup><a id="fn.32" class="footnum" href="#fnr.32">32</a></sup> And some example footnote</p>
<p><sup><a id="fn.33" class="footnum" href="#fnr.33">33</a></sup> And some example footnote</p>
<p><sup><a id="fn.34" class="footnum" href="#fnr.34">34</a></sup> And some example footnote</p>
<p><sup><a id="fn.35" class="footnum" href="#fnr.35">35</a></sup> And some example footnote</p>
<p><sup><a id="fn.36" class="footnum" href="#fnr.36">36</a></sup> And some example footnote</p>
<p><sup><a id="fn.37" class="footnum" href="#fnr.37">37</a></sup> And some example footnote</p>
<p><sup><a id="fn.38" class="footnum" href="#fnr.38">38</a></sup> And some example footnote</p>
<p><sup><a id="fn.39" class="footnum" href="#fnr.39">39</a></sup> And some example footnote</p>
<p><sup><a id="fn.40" class="footnum" href="#fnr.40">40</a></sup> And some example footnote</p>
<p><sup><a id="fn.41" class="footnum" href="#fnr.41">41</a></sup> And some example footnote</p>
<p><sup><a id="fn.42" class="footnum" href="#fnr.42">42</a></sup> And some example footnote</p>
<p><sup><a id="fn.43" class="footnum" href="#fnr.43">43</a></sup> And some example footnote</p>
<p><sup><a id="fn.44" class="footnum" href="#fnr.44">44</a></sup> And some example footnote</p>
<p><sup><a id="fn.45" class="footnum" href="#fnr.45">45</a></sup> And some example footnote</p>
<p><sup><a id="fn.46" class="footnum" href="#fnr.46">46</a></sup> And some example footnote</p>
<p><sup><a id="fn.47" class="footnum" href="#fnr.47">47</a></sup> And some example footnote</p>
<p><sup><a id="fn.48" class="footnum" href="#fnr.48">48</a></sup> And some example footnote</p>
<p><sup><a id="fn.49" class="footnum" href="#fnr.49">49</a></sup> And some example footnote</p>
<p><sup><a id="fn.50" class="footnum" href="#fnr.50">50</a></sup> And some example footnote</p>
<p>Using letters and defined in the footnote <sup><a id="fnr.abc" class="footref" href="#fn.abc">abc</a></sup></p>
<div id="footnotes">
<h2 class="footnotes">Footnotes Title</h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.0" href="#fnr.0">0</a></sup><p class="footpara"><b>blub</b></p></div>
<div class="footdef"><sup><a id="fn.abc" href="#fnr.abc">abc</a></sup><p class="footpara">definition of abc</p></div>
</div>
</div>
1 change: 0 additions & 1 deletion spec/org-ruby/html_output_buffer_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# coding: utf-8
require 'spec_helper'
require_relative '../../lib/org-ruby/html_output_buffer'

Expand Down
Loading

0 comments on commit 1f72e6b

Please sign in to comment.