Skip to content

Commit

Permalink
resolves #4556 set cloaked-context attribute on source block when con…
Browse files Browse the repository at this point in the history
…text is not :listing (PR #4563)
  • Loading branch information
mojavelinux committed Mar 8, 2024
1 parent 02cfbde commit 7ce2240
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Expand Up @@ -66,6 +66,7 @@ Improvements::
* Remove empty line at top of table cells in manpage output (#4482) (*@strager*)
* Return `nil` if name passed to `Asciidoctor::SafeMode.value_for_name` is not recognized (#3526)
* Modify default stylesheet to honor text-* roles on quote blocks
* Set `cloaked-context` attribute on source block when context is not `:listing` (#4556)

== 2.0.21 (2024-02-20) - @mojavelinux

Expand Down
7 changes: 5 additions & 2 deletions lib/asciidoctor/parser.rb
Expand Up @@ -562,6 +562,7 @@ def self.next_block reader, parent, attributes = {}, options = {}
# process lines verbatim
if style && Compliance.strict_verbatim_paragraphs && (VERBATIM_STYLES.include? style)
block_context = style.to_sym
cloaked_context = :paragraph
reader.unshift_line this_line
# advance to block parsing =>
break
Expand Down Expand Up @@ -820,15 +821,16 @@ def self.next_block reader, parent, attributes = {}, options = {}
case block_context
when :listing, :source
if block_context == :source || (language = attributes[1] ? nil : attributes[2] || doc_attrs['source-language'])
if language
if language # :listing
attributes['style'] = 'source'
attributes['language'] = language
AttributeList.rekey attributes, [nil, nil, 'linenums']
else
else # :source
AttributeList.rekey attributes, [nil, 'language', 'linenums']
if doc_attrs.key? 'source-language'
attributes['language'] = doc_attrs['source-language']
end unless attributes.key? 'language'
attributes['cloaked-context'] = cloaked_context unless cloaked_context == :listing
end
if (attributes.key? 'linenums') || (doc_attrs.key? 'source-linenums-option')
attributes['linenums-option'] = ''
Expand Down Expand Up @@ -857,6 +859,7 @@ def self.next_block reader, parent, attributes = {}, options = {}
else
attributes['language'] = language
end
attributes['cloaked-context'] = cloaked_context
if (attributes.key? 'linenums') || (doc_attrs.key? 'source-linenums-option')
attributes['linenums-option'] = ''
end unless attributes.key? 'linenums-option'
Expand Down
50 changes: 50 additions & 0 deletions test/blocks_test.rb
Expand Up @@ -3557,6 +3557,11 @@ def names
```
EOS

block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :fenced_code, (block.attr 'cloaked-context')
assert_nil (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 1
assert_css '.listingblock pre code', output, 1
Expand Down Expand Up @@ -3589,6 +3594,11 @@ def names
```
EOS

block = (document_from_string input).blocks[0]
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :fenced_code, (block.attr 'cloaked-context')
assert_equal 'ruby', (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 2
assert_css '.listingblock pre code.language-ruby[data-lang=ruby]', output, 1
Expand All @@ -3611,6 +3621,46 @@ def names
assert_css '.listingblock pre code.language-ruby[data-lang=ruby]', output, 1
assert_css '.listingblock pre code.language-javascript[data-lang=javascript]', output, 1
end

test 'should allow source style to be specified on literal block' do
input = <<~'EOS'
[source]
....
console.log('Hello, World!')
....
EOS

block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :literal, (block.attr 'cloaked-context')
assert_nil (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 1
assert_css '.listingblock pre', output, 1
assert_css '.listingblock pre code', output, 1
assert_css '.listingblock pre code[data-lang]', output, 0
end

test 'should allow source style and language to be specified on literal block' do
input = <<~'EOS'
[source,js]
....
console.log('Hello, World!')
....
EOS

block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :literal, (block.attr 'cloaked-context')
assert_equal 'js', (block.attr 'language')
output = convert_string_to_embedded input
assert_css '.listingblock', output, 1
assert_css '.listingblock pre', output, 1
assert_css '.listingblock pre code', output, 1
assert_css '.listingblock pre code[data-lang]', output, 1
end
end

context 'Abstract and Part Intro' do
Expand Down
10 changes: 10 additions & 0 deletions test/paragraphs_test.rb
Expand Up @@ -326,6 +326,11 @@
[source]
use the source, luke!
EOS
block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :paragraph, (block.attr 'cloaked-context')
assert_nil (block.attr 'language')
output = convert_string_to_embedded input
assert_xpath %(/*[@class="listingblock"]//pre[@class="highlight"]/code[text()="use the source, luke!"]), output, 1
end
Expand All @@ -335,6 +340,11 @@
[source, perl]
die 'zomg perl is tough';
EOS
block = block_from_string input
assert_equal :listing, block.context
assert_equal 'source', (block.attr 'style')
assert_equal :paragraph, (block.attr 'cloaked-context')
assert_equal 'perl', (block.attr 'language')
output = convert_string_to_embedded input
assert_xpath %(/*[@class="listingblock"]//pre[@class="highlight"]/code[@class="language-perl"][@data-lang="perl"][text()="die 'zomg perl is tough';"]), output, 1
end
Expand Down

0 comments on commit 7ce2240

Please sign in to comment.