Skip to content

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

Merged
merged 6 commits into from
Mar 14, 2019

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Mar 7, 2019

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.

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.
@nik9000 nik9000 requested a review from estolfo March 7, 2019 19:37
@nik9000
Copy link
Member Author

nik9000 commented Mar 7, 2019

@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.

Copy link
Contributor

@estolfo estolfo left a 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?
Copy link
Contributor

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

Copy link
Member Author

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
Copy link
Contributor

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.

@nik9000
Copy link
Member Author

nik9000 commented Mar 12, 2019

@estolfo, I pushed a change for the nil? thing and a change to use context, let, and subject for that section. If it looks right to you I can do the other sections as I get there.

Copy link
Contributor

@estolfo estolfo left a 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
Copy link
Contributor

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.

@nik9000
Copy link
Member Author

nik9000 commented Mar 13, 2019

@estolfo, I pushed some changes to the rspec to line up a bit better with what you proposed.

Copy link
Contributor

@estolfo estolfo left a 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.

@nik9000 nik9000 merged commit c2fb809 into elastic:master Mar 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants