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 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) 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 = "" 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