From 881b0e6af09dc8ee84d645b2c0cff59ff320cc4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ib=C3=A1=C3=B1ez?= Date: Wed, 11 May 2022 20:53:14 +0200 Subject: [PATCH 1/4] Add heading id generation code logic --- lib/org-ruby/html_output_buffer.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/org-ruby/html_output_buffer.rb b/lib/org-ruby/html_output_buffer.rb index 180bd80..b151fc2 100644 --- a/lib/org-ruby/html_output_buffer.rb +++ b/lib/org-ruby/html_output_buffer.rb @@ -35,6 +35,7 @@ def initialize(output, opts = {}) @new_paragraph = :start @footnotes = {} @unclosed_tags = [] + @heading_id = [] @logger.debug "HTML export options: #{@options.inspect}" @custom_blocktags = {} if @options[:markup_file] @@ -195,6 +196,13 @@ def flush! end def add_line_attributes headline + if @options[:generate_heading_id] then + level = headline.level + @output.delete_suffix!('>') + @heading_id.slice!(level, @heading_id.length - level) + @heading_id[level-1]=headline.output_text.downcase.gsub(/\W/, "-") + @output << " id=\"" + @heading_id.join("--") + "\">" + end if @options[:export_heading_number] then level = headline.level heading_number = get_next_headline_number(level) From 93ac76854d7cf341b8ebbabe6655d9cb0e4e75ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ib=C3=A1=C3=B1ez?= Date: Wed, 11 May 2022 20:54:39 +0200 Subject: [PATCH 2/4] Set generate_heading_id option from the parser --- lib/org-ruby/parser.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/org-ruby/parser.rb b/lib/org-ruby/parser.rb index a57fd9e..eff8cc3 100644 --- a/lib/org-ruby/parser.rb +++ b/lib/org-ruby/parser.rb @@ -346,7 +346,8 @@ def to_html :export_footnotes => export_footnotes?, :link_abbrevs => @link_abbrevs, :skip_syntax_highlight => @parser_options[:skip_syntax_highlight], - :markup_file => @parser_options[:markup_file] + :markup_file => @parser_options[:markup_file], + :generate_heading_id => @parser_options[:generate_heading_id] } export_options[:skip_tables] = true if not export_tables? output = "" From dee5ee29cc55c4f699034ef7b9c7cc2c738315da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ib=C3=A1=C3=B1ez?= Date: Wed, 11 May 2022 20:55:39 +0200 Subject: [PATCH 3/4] Add -i flag to org-ruby to enable the HTML headings id generation --- bin/org-ruby | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/org-ruby b/bin/org-ruby index 1b4e5e4..71b01c1 100755 --- a/bin/org-ruby +++ b/bin/org-ruby @@ -17,6 +17,10 @@ options_parser = OptionParser.new do |opts| options[:debug] = true end + opts.on("-i", "--heading-id", "Add id attribute to HTML headings") do |v| + options[:generate_heading_id] = true + end + opts.on("-m", "--markup ", "Set Custom Markup file") do |f| options[:markup_file] = f end @@ -43,7 +47,10 @@ begin puts options_parser else data = IO.read(ARGV[0]) - p = Orgmode::Parser.new(data, (options[:markup_file] ? {:markup_file => options[:markup_file]} : {})) + parser_options = {} + parser_options.merge!(options[:markup_file] ? {:markup_file => options[:markup_file]} : {}) + parser_options.merge!(options[:generate_heading_id] ? {:generate_heading_id => options[:generate_heading_id]} : {}) + p = Orgmode::Parser.new(data, parser_options) $DEBUG = true if options[:debug] puts p.to_html if options[:format] == :html puts p.to_textile if options[:format] == :textile From 38846e66f5339368e872c272fbe7bf8d23d7776b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Ib=C3=A1=C3=B1ez?= Date: Wed, 11 May 2022 21:18:45 +0200 Subject: [PATCH 4/4] Add spec file to check for the generate_heading_id option --- spec/html_output_buffer_spec.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 spec/html_output_buffer_spec.rb diff --git a/spec/html_output_buffer_spec.rb b/spec/html_output_buffer_spec.rb new file mode 100644 index 0000000..c86d57a --- /dev/null +++ b/spec/html_output_buffer_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe Orgmode::HtmlOutputBuffer do + it 'generates html headings ids' do + lines = "* Hello\n** World" + parser_options = {:generate_heading_id => true} + expected_output = "

Hello

\n

World

" + actual_output = Orgmode::Parser.new(lines, parser_options).to_html.strip + expect(actual_output).to eql(expected_output) + end +end