-
Notifications
You must be signed in to change notification settings - Fork 345
Asciidoctor: Handle snippets in definition lists #680
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
Asciidoctor: Handle snippets in definition lists #680
Conversation
Our lang override processor had trouble with snippets inside of definition lists because a method provided by Asciidoctor doesn't quite behave well inside definition lists: asciidoctor/asciidoctor#3133 That method had behavior that is a little too broad anyway so we implement what we need directly.
@estolfo could you have a look at this one? It is 100% ruby and small enough that I think we can iterate on some best practices things here. This in particular makes me like the whole "early return" thing so we don't end up with a ton of indentation. I know this has issues with the magic perl variables, but I think it'd be best to clean those up separately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have any major comments, just some minor suggestions, one of which might be out of the scope of this PR.
# We don't want block.next_adjacent_block because that'll go "too far" | ||
# and it has trouble with definition lists. | ||
next_block_index = block.parent.blocks.find_index block | ||
return if next_block_index.nil? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe it would be more idiomatic to write return unless next_block_index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When I wrote it I felt like "This can only be nil or a string so I should be explicit" but I've come around to your way.
@@ -96,4 +96,20 @@ | |||
expect(actual).to eq(expected.strip) | |||
end | |||
end | |||
|
|||
it "doesn't mind when the snippet inside a definition list" do | |||
actual = convert <<~ASCIIDOC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I mentioned this in the last PR I reviewed, maybe eventually you can use context
and let
blocks instead in the specs.
For example, this case would have a context when the snippet is inside a definition list
and then the it
block would be generates the correct string
(or whatever). The actual
and expected
variables would be let
blocks.
But I think that change would be part of a larger rspec refactor and perhaps out of the scope of this PR.
This is an example of how context blocks and let blocks can be used.
@estolfo, I pushed a change for the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added some rspec comments : )
@@ -96,4 +96,27 @@ | |||
expect(actual).to eq(expected.strip) | |||
end | |||
end | |||
|
|||
context 'a snippet is inside of a definition list' do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good step in the direction of idiomatic Rspec : )
I always favor let
instead of subject
because it's named. The benefit of this, and of using let
in general, is so that you can write embedded tests that reuse the "variables". (I put that in quotes because it's actually a method that creates the memoized variable on the fly, when it's called)
So you could write this as
let(:converted) do
convert <<~ASCIIDOC
== Example
Term::
Definition
+
--
[source,js]
----
GET /
----
--
ASCIIDOC
end
let(:regexp) do
%r{<programlisting language="js" linenumbering="unnumbered">GET /</programlisting>}
end
it "converts the string" do
expect(converted).to match(regexp)
end
Then, if you wanted to write another test using that converted string, you could easily add something like this below the first it
block:
it "doesn't match pizza" do
expect(converted).not_to match(/pizza/)
end
and you can also nest context blocks that reuse the converted
string:
let(:converted) do
convert <<~ASCIIDOC
== Example
Term::
Definition
+
--
[source,js]
----
GET /
----
--
ASCIIDOC
end
let(:regexp) do
%r{<programlisting language="js" linenumbering="unnumbered">GET /</programlisting>}
end
it "converts the string" do
expect(converted).to match(regexp)
end
context "when it's raining" do
let(:regexp) do
# And you can override any "variables" you need to!
/sun/
end
it "converts the string" do
expect(converted).not_to match(regexp)
end
end
The converted
string will only ever be created once, even when you use it repeatedly in tests.
@estolfo, I pushed some changes to the rspec to line up a bit better with what you proposed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! The rspec looks good now.
Our lang override processor had trouble with snippets inside of
definition lists because a method provided by Asciidoctor doesn't quite
behave well inside definition lists:
asciidoctor/asciidoctor#3133
That method had behavior that is a little too broad anyway so we
implement what we need directly.