diff --git a/lib/org-ruby/html_output_buffer.rb b/lib/org-ruby/html_output_buffer.rb index 021abfc..d76b746 100644 --- a/lib/org-ruby/html_output_buffer.rb +++ b/lib/org-ruby/html_output_buffer.rb @@ -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 @@ -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("") - @list_indent_stack.pop + list_indent_stack.pop end def highlight(code, lang) @@ -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 @@ -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
\n

Footnotes:

\n
\n" @footnotes.each do |name, (defi, content)| @buffer = defi @output << "
#{name}" \ @@ -219,11 +223,40 @@ def output_footnotes! return true end + def footnotes_header + footnotes_title = options[:footnotes_title] || "Footnotes:" + "\n
\n

#{footnotes_title}

\n
\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 @@ -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 = { @@ -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}" + escaped_text = escapeHTML(text) + "<#{Tags[marker][:open]}>#{escaped_text}" else - quote_tags("<#{Tags[marker][:open]}>") + s + + quote_tags("<#{Tags[marker][:open]}>") + text + quote_tags("") end end diff --git a/lib/org-ruby/output_buffer.rb b/lib/org-ruby/output_buffer.rb index 7dd3ca7..f5b621c 100644 --- a/lib/org-ruby/output_buffer.rb +++ b/lib/org-ruby/output_buffer.rb @@ -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 diff --git a/lib/org-ruby/parser.rb b/lib/org-ruby/parser.rb index c9d2624..40e198a 100644 --- a/lib/org-ruby/parser.rb +++ b/lib/org-ruby/parser.rb @@ -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 @@ -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 = "" @@ -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 diff --git a/lib/org-ruby/regexp_helper.rb b/lib/org-ruby/regexp_helper.rb index 9606338..7e79317 100644 --- a/lib/org-ruby/regexp_helper.rb +++ b/lib/org-ruby/regexp_helper.rb @@ -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" diff --git a/spec/html_examples/footnotes_title.html b/spec/html_examples/footnotes_title.html new file mode 100644 index 0000000..bd563b7 --- /dev/null +++ b/spec/html_examples/footnotes_title.html @@ -0,0 +1,111 @@ +

Footnotes

+

Using letters and not defined in the footnote abc

+

Defined in the footnote itself with markup 0

+

Some example paragraph 1, with text at the end.

+

Some example paragraph 2, with text at the end.

+

Some example paragraph 3, with text at the end.

+

Some example paragraph 4, with text at the end.

+

Some example paragraph 5, with text at the end.

+

Some example paragraph 6, with text at the end.

+

Some example paragraph 7, with text at the end.

+

Some example paragraph 8, with text at the end.

+

Some example paragraph 9, with text at the end.

+

Some example paragraph 10, with text at the end.

+

Some example paragraph 11, with text at the end.

+

Some example paragraph 12, with text at the end.

+

Some example paragraph 13, with text at the end.

+

Some example paragraph 14, with text at the end.

+

Some example paragraph 15, with text at the end.

+

Some example paragraph 16, with text at the end.

+

Some example paragraph 17, with text at the end.

+

Some example paragraph 18, with text at the end.

+

Some example paragraph 19, with text at the end.

+

Some example paragraph 20, with text at the end.

+

Some example paragraph 21, with text at the end.

+

Some example paragraph 22, with text at the end.

+

Some example paragraph 23, with text at the end.

+

Some example paragraph 24, with text at the end.

+

Some example paragraph 25, with text at the end.

+

Some example paragraph 26, with text at the end.

+

Some example paragraph 27, with text at the end.

+

Some example paragraph 28, with text at the end.

+

Some example paragraph 29, with text at the end.

+

Some example paragraph 30, with text at the end.

+

Some example paragraph 31, with text at the end.

+

Some example paragraph 32, with text at the end.

+

Some example paragraph 33, with text at the end.

+

Some example paragraph 34, with text at the end.

+

Some example paragraph 35, with text at the end.

+

Some example paragraph 36, with text at the end.

+

Some example paragraph 37, with text at the end.

+

Some example paragraph 38, with text at the end.

+

Some example paragraph 39, with text at the end.

+

Some example paragraph 40, with text at the end.

+

Some example paragraph 41, with text at the end.

+

Some example paragraph 42, with text at the end.

+

Some example paragraph 43, with text at the end.

+

Some example paragraph 44, with text at the end.

+

Some example paragraph 45, with text at the end.

+

Some example paragraph 46, with text at the end.

+

Some example paragraph 47, with text at the end.

+

Some example paragraph 48, with text at the end.

+

Some example paragraph 49, with text at the end.

+

Some example paragraph 50, with text at the end.

+

1 And some example footnote

+

2 And some example footnote

+

3 And some example footnote

+

4 And some example footnote

+

5 And some example footnote

+

6 And some example footnote

+

7 And some example footnote

+

8 And some example footnote

+

9 And some example footnote

+

10 And some example footnote

+

11 And some example footnote

+

12 And some example footnote

+

13 And some example footnote

+

14 And some example footnote

+

15 And some example footnote

+

16 And some example footnote

+

17 And some example footnote

+

18 And some example footnote

+

19 And some example footnote

+

20 And some example footnote

+

21 And some example footnote

+

22 And some example footnote

+

23 And some example footnote

+

24 And some example footnote

+

25 And some example footnote

+

26 And some example footnote

+

27 And some example footnote

+

28 And some example footnote

+

29 And some example footnote

+

30 And some example footnote

+

31 And some example footnote

+

32 And some example footnote

+

33 And some example footnote

+

34 And some example footnote

+

35 And some example footnote

+

36 And some example footnote

+

37 And some example footnote

+

38 And some example footnote

+

39 And some example footnote

+

40 And some example footnote

+

41 And some example footnote

+

42 And some example footnote

+

43 And some example footnote

+

44 And some example footnote

+

45 And some example footnote

+

46 And some example footnote

+

47 And some example footnote

+

48 And some example footnote

+

49 And some example footnote

+

50 And some example footnote

+

Using letters and defined in the footnote abc

+
+

Footnotes Title

+
+
0

blub

+
abc

definition of abc

+
+
diff --git a/spec/org-ruby/html_output_buffer_spec.rb b/spec/org-ruby/html_output_buffer_spec.rb index 445d142..88866d2 100644 --- a/spec/org-ruby/html_output_buffer_spec.rb +++ b/spec/org-ruby/html_output_buffer_spec.rb @@ -1,4 +1,3 @@ -# coding: utf-8 require 'spec_helper' require_relative '../../lib/org-ruby/html_output_buffer' diff --git a/spec/org-ruby/output_buffer_spec.rb b/spec/org-ruby/output_buffer_spec.rb index 6dcafe1..ea81f29 100644 --- a/spec/org-ruby/output_buffer_spec.rb +++ b/spec/org-ruby/output_buffer_spec.rb @@ -3,8 +3,36 @@ require_relative '../../lib/org-ruby/output_buffer' module Orgmode RSpec.describe OutputBuffer do + let(:output) { 'Output' } + let(:buffer) { Orgmode::OutputBuffer.new(output) } + describe '.new' do + it 'receives and has an output' do + expect(buffer.output).to eq output + end + end + + describe 'get_next_headline_number' do + let(:level) { 1 } + context 'when @headline_number_stack is empty' do + it 'returns 1' do + headline_number = buffer.get_next_headline_number(level) + expect(headline_number).to eq '1' + end + end + + context 'when @headline_number_stack has numbers' do + let(:level) { 2 } + before(:each) do + buffer.headline_number_stack.push(4) + buffer.headline_number_stack.push(2) + end + it 'returns 4.3' do + headline_number = buffer.get_next_headline_number(level) + expect(headline_number).to eq '4.3' + end + end end end end diff --git a/spec/parser_spec.rb b/spec/parser_spec.rb index 55aeb20..af2207e 100644 --- a/spec/parser_spec.rb +++ b/spec/parser_spec.rb @@ -329,6 +329,19 @@ end end + describe "Footnotes title" do + base_dir = File.join(File.dirname(__FILE__), "html_examples") + org_file = File.join(base_dir, "footnotes.org") + html_file = File.join(base_dir, "footnotes_title.html") + it 'accept footnotes_title option' do + expected = IO.read(html_file) + parser = Orgmode::Parser.new(IO.read(org_file), :footnotes_title => "Footnotes Title") + actual = parser.to_html + expect(actual).to be_kind_of(String) + expect(actual).to eq(expected) + end + end + describe "Export to Markdown test cases" do data_directory = File.join(File.dirname(__FILE__), "markdown_examples") org_files = File.expand_path(File.join(data_directory, "*.org" )) @@ -489,4 +502,3 @@ end end end -