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

Markup agnostic extra files #1200

Open
wants to merge 2 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
17 changes: 10 additions & 7 deletions lib/yard/code_objects/extra_file_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def ensure_parsed
# @param [String] data the file contents
def parse_contents(data)
retried = false
cut_index = 0
cut_index_begin = 0
cut_index_end = 0
data = translate(data)
data = data.split("\n")
data.each_with_index do |line, index|
Expand All @@ -89,19 +90,21 @@ def parse_contents(data)
if index == 0
attributes[:markup] = $1
else
cut_index = index
cut_index_end = index
break
end
when /^\s*#\s*@(\S+)\s*(.+?)\s*$/
attributes[$1] = $2
when /^\s*<!--\s*$/, /^\s*-->\s*$/
# Ignore HTML comments
else
cut_index = index
break
if index == 0
cut_index_begin = 1
else
cut_index_end = index
break
end
end
end
data = data[cut_index..-1] if cut_index > 0
data[cut_index_begin...cut_index_end] = [] if cut_index_end > cut_index_begin
contents = data.join("\n")

if contents.respond_to?(:force_encoding) && attributes[:encoding]
Expand Down
22 changes: 17 additions & 5 deletions spec/code_objects/extra_file_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,25 @@
it "allows the attributes section to be wrapped in an HTML comment" do
file = ExtraFileObject.new('file.txt', "<!--\n# @title X\n-->\nFOO BAR")
expect(file.attributes[:title]).to eq "X"
expect(file.contents).to eq "FOO BAR"
expect(file.contents).to eq "<!--\n-->\nFOO BAR"
end

it "allows whitespace around ignored HTML comment" do
file = ExtraFileObject.new('file.txt', " \t <!-- \n# @title X\n \t --> \nFOO BAR")
expect(file.attributes[:title]).to eq "X"
expect(file.contents).to eq "FOO BAR"
expect(file.contents).to eq " \t <!-- \n \t --> \nFOO BAR"
end

it "allows the attributes section to be wrapped in an AsciiDoc comment" do
file = ExtraFileObject.new('file.txt', "````\n# @title X\n````\nFOO BAR")
expect(file.attributes[:title]).to eq "X"
expect(file.contents).to eq "````\n````\nFOO BAR"
end

it "allows the attributes section to be wrapped in a Textile comment" do
file = ExtraFileObject.new('file.txt', "###.\n# @title X\n\nFOO BAR")
expect(file.attributes[:title]).to eq "X"
expect(file.contents).to eq "###.\n\nFOO BAR"
end

it "parses out old-style #!markup shebang format" do
Expand All @@ -58,10 +70,10 @@
expect(file.contents).to eq " #!foobar\nHello"
end

it "does not parse out attributes if there are newlines prior to attributes" do
file = ExtraFileObject.new('file.txt', "\n# @title\nFOO BAR")
it "does not parse out attributes if there are at least two newlines prior to attributes" do
file = ExtraFileObject.new('file.txt', "\n\n# @title\nFOO BAR")
expect(file.attributes).to be_empty
expect(file.contents).to eq "\n# @title\nFOO BAR"
expect(file.contents).to eq "\n\n# @title\nFOO BAR"
end

it "sets contents to data after attributes" do
Expand Down