Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only add a name section if one doesn't already exist #115

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/ronn/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,32 @@ def sniff
html = Kramdown::Document.new(data[0, 512], auto_ids: false,
smart_quotes: ['apos', 'apos', 'quot', 'quot'],
typographic_symbols: { hellip: '...', ndash: '--', mdash: '--' }).to_html
sniff_h2_headings(html) or sniff_h1_heading(html) or [nil, nil, nil]
end

# If the document has a '## NAME' heading, see if we can sniff out
# some of the document metadata.
def sniff_h2_headings(html)
html.split('<h2>').each do |section|
case section
when /^NAME<\/h2>\s*<p>([\w_.\/\[\]~+=@:<>-]+)\s+-+\s+([\w_.\/\[\]~+=@: -]*)<\/p>/m
# name -- description
description = $2
name = $1.gsub(/<[^>]+>/, '')
return [name, nil, description]
when /^NAME<\/h2>\s*<p>([\w_.\/\[\]~+=@:<>-]+)<\/p>/m
# name
return [$1.gsub(/<[^>]+>/, ''), nil, nil]
end
end
nil
end

# If the document has a top-level '# <data>' type heading, see
# what kind of metadata we can sniff out of it.
def sniff_h1_heading(html)
heading, html = html.split("</h1>\n", 2)
return [nil, nil, nil] if html.nil?
return if html.nil?

case heading
when /([\w_.\[\]~+=@:-]+)\s*\((\d\w*)\)\s*-+\s*(.*)/
Expand Down Expand Up @@ -436,7 +460,7 @@ def html_filter_inject_name_section
markup =
if title?
"<h1>#{title}</h1>"
elsif name
elsif name && !@html.css('h2').map(&:text).include?('NAME')
"<h2>NAME</h2>\n" \
"<p class='man-name'>\n <code>#{name}</code>" +
(tagline ? " - <span class='man-whatis'>#{tagline}</span>\n" : "\n") +
Expand Down
9 changes: 9 additions & 0 deletions test/existing_name_section.ronn
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Test #

## NAME ##

test - the test manpage

## DESCRIPTION ##

Testing items. w00t!
17 changes: 17 additions & 0 deletions test/test_ronn_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ def canonicalize(text)
assert_equal '5', doc.section
assert_equal 'wootderitis', doc.tagline
end

test "new with NAME heading with #{i} dashes and description" do
doc = Ronn::Document.new { "# whatever\n\n## NAME\n\n`foo` #{dashes} bar" }
assert_equal 'foo', doc.name
assert_equal 'bar', doc.tagline
end
end

test 'new with NAME heading without description' do
doc = Ronn::Document.new { "# whatever\n\n## NAME\n\n`foo`" }
assert_equal 'foo', doc.name
assert_equal nil, doc.tagline
end

context 'simple conventionally named document' do
Expand Down Expand Up @@ -188,4 +200,9 @@ def canonicalize(text)
@doc = Ronn::Document.new('hello.1.ronn', styles: %w[test boom test]) { '' }
assert_equal %w[man test boom], @doc.styles
end

test 'NAME section is not duplicated' do
html = Ronn::Document.new(File.expand_path('existing_name_section.ronn', __dir__)).to_html
assert html.scan(/<h2[^>]*>NAME<\/h2>/).length == 1
end
end