Skip to content

Commit

Permalink
#32 spec
Browse files Browse the repository at this point in the history
  • Loading branch information
xyz65535 committed Sep 4, 2024
1 parent 4a517b5 commit b0d50b6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 12 deletions.
10 changes: 5 additions & 5 deletions lib/coradoc/element/tag.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
module Coradoc
module Element
class Tag < Base
attr_accessor :name
attr_accessor :name, :prefix, :attrs, :line_break

def initialize(tag_name, options = {})
@tag_name = tag_name
def initialize(name, options = {})
@name = name
@prefix = options.fetch(:prefix, "tag")
@attrs = options.fetch(:attribute_list, nil)
@attrs = options.fetch(:attribute_list, AttributeList.new)
@line_break = options.fetch(:line_break, "\n")
end

def to_adoc
attrs = @attrs.to_adoc
"// #{@prefix}::#{@tag_name}#{attrs}#{@line_break}"
"// #{@prefix}::#{@name}#{attrs}#{@line_break}"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/coradoc/parser/asciidoc/attribute_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def positional_zero_or_one
end

def attribute_list(name = :attribute_list)
match('^\[') >> str("[").absent? >>
str('[') >> str("[").absent? >>
( named_many |
positional_one_named_many |
positional_many_named_many |
Expand Down
10 changes: 6 additions & 4 deletions lib/coradoc/parser/asciidoc/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -141,19 +141,21 @@ def block_image
end

def comment_line
tag.absent? >>
(str('//') >> str("/").absent? >>
space? >>
text.as(:comment_text)
).as(:comment_line)
end

def tag
(str('//') >> str("/").absent? >>
(str('//') >> str('/').absent? >>
space? >>
(str('tag') | str('end')).as(:prefix) >>
str('::') >>
text.as(:text) >>
attribute_list
str('::') >> str(':').absent? >>
match('[^\[]').repeat(1).as(:name) >>
attribute_list >>
line_ending.maybe.as(:line_break)
).as(:tag)
end

Expand Down
6 changes: 5 additions & 1 deletion lib/coradoc/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ class Transformer < Parslet::Transform
}

rule(tag: subtree(:tag)) {
Element::Tag.new(tag[:name])
opts = {}
opts[:prefix] = tag[:prefix]
opts[:attribute_list] = tag[:attribute_list]
opts[:line_break] = tag[:line_break]
Element::Tag.new(tag[:name], opts)
}

# AttributeList
Expand Down
17 changes: 17 additions & 0 deletions spec/coradoc/element/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "spec_helper"

RSpec.describe Coradoc::Element::Tag do
describe ".initialize" do
it "initializes and exposes tag" do
name = "name"
opts = {prefix: "tag",
attribute_list: Coradoc::Element::AttributeList.new,
line_break: "\n"}
tag = Coradoc::Element::Tag.new(name, opts)
expect(tag.name).to eq(name)
expect(tag.prefix).to eq("tag")
expect(tag.attrs.class).to eq(Coradoc::Element::AttributeList)
expect(tag.line_break).to eq("\n")
end
end
end
2 changes: 1 addition & 1 deletion spec/coradoc/parser/asciidoc/citation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

RSpec.describe "Coradoc::Parser::Asciidoc::Citatione" do
describe ".parse" do
it "parses various inline text formattings" do
it "parses multiple types of citation" do
parser = Asciidoc::CitationTester


Expand Down
35 changes: 35 additions & 0 deletions spec/coradoc/parser/asciidoc/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "spec_helper"

RSpec.describe "Coradoc::Parser::Asciidoc::Tag" do
describe ".parse" do
it "parses tags" do
parser = Asciidoc::TagTester


ast = parser.parse("// tag::name[]\n")
expect(ast).to eq([{:tag=>{:prefix=>"tag", :name=>"name", :attribute_list=>{:attribute_array=>[]}, :line_break=>"\n"}}])

ast = parser.parse("// end::name[]\n")
expect(ast).to eq([{:tag=>{:prefix=>"end", :name=>"name", :attribute_list=>{:attribute_array=>[]}, :line_break=>"\n"}}])

ast = parser.parse("// tag::name[]")
expect(ast).to eq([{:tag=>{:prefix=>"tag", :name=>"name", :attribute_list=>{:attribute_array=>[]}, :line_break=>nil}}])


end
end
end


module Asciidoc
class TagTester < Parslet::Parser
include Coradoc::Parser::Asciidoc::Base

rule(:document) { (tag | any.as(:unparsed)).repeat(1) }
root :document

def self.parse(text)
new.parse_with_debug(text)
end
end
end

0 comments on commit b0d50b6

Please sign in to comment.