Skip to content

Commit

Permalink
(PDOC-36) hack to fix README links in generated HTML
Browse files Browse the repository at this point in the history
  • Loading branch information
eputnam committed Mar 22, 2018
1 parent 67f249c commit 37cfe49
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ def layout
@path = object.path
end

erb(:layout)
final_layout = erb(:layout)

if @file && @file.name == 'README'
PuppetStrings::Yard::Util.github_to_yard_links(final_layout)
end

final_layout
end

# Creates the dynamic menu lists.
Expand Down
14 changes: 14 additions & 0 deletions lib/puppet-strings/yard/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ def self.scrub_string(str)

Puppet::Util::Docs.scrub(str)
end

# hacksville, usa
# YARD creates ids in the html with with the style of "label-Module+description", where the markdown
# we use in the README involves the GitHub-style, which is #module-description. This takes our GitHub-style
# links and converts them to reference the YARD-style ids.
# @see https://github.com/octokit/octokit.rb/blob/0f13944e8dbb0210d1e266addd3335c6dc9fe36a/yard/default/layout/html/setup.rb#L5-L14
# @param [String] data HTML document to convert
# @return [String] HTML document with links converted
def self.github_to_yard_links(data)
data.scan(/href\=\"\#(.+)\"/).each do |bad_link|
data.gsub!(bad_link.first, "label-#{bad_link.first.capitalize.gsub('-', '+')}")
end
data
end
end
12 changes: 12 additions & 0 deletions spec/unit/puppet-strings/yard/util_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,16 @@
expect(subject.scrub_string(str)).to eq('this is a test string')
end
end

describe 'github_to_yard_links' do
it 'converts a link correctly' do
str = '<a href="#module-description">'
expect(subject.github_to_yard_links(str)).to eq('<a href="#label-Module+description">')
end

it 'leaves other links with hashes alone' do
str = '<a href="github.com/blah/document.html#module-description">'
expect(subject.github_to_yard_links(str)).to eq(str)
end
end
end

0 comments on commit 37cfe49

Please # to comment.