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

Fancy find #10

Open
wants to merge 2 commits into
base: master
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
19 changes: 18 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ You can further customize the source block output with additional Rouge attribut

rouge-css::
Controls what method is used for applying CSS to the tokens.
Can be `class` (CSS classes) or `style` (inline styles).
Can be `class` (CSS classes), `style` (inline styles) or `external` (external styles).
When `class` is used, Rouge styles for the specified theme are included in an HTML header.
When `external` is specified, CSS classes are used but no styles will be added except when `rouge-theme` is not empty in which case its value is interpreted as a URL to a style sheet and a link to that style sheet will be added to the HTML header.
Default is `class`.

rouge-theme::
Expand All @@ -63,6 +64,22 @@ rouge-style::
Alternative name for the `rouge-theme` for compatibility with _asciidoctor-pdf_ (see https://github.com/{gh-name}/issues/3[#3]).


=== Passing CGI-style options for selected language

Rouge allows passing https://github.com/jneen/rouge#you-can-even-use-it-with-redcarpet[certain options] to its Lexer classes when selecting the language to parse. This feature is also available from within Asciidoctor. So instead of simply writing:

```
[source, console]
```

to select `console` as output language (with its default options) you can also write:

```
[source, "console?prompt=$> "]
```

This will also select `console` as output language but it will additionally set the prompt string to ``$> ``. Options are comma separeted `key=value` pairs which are appended to the language string with a `?`-character. Consult the various https://www.rubydoc.info/gems/rouge/Rouge/Lexers[Rouge Lexer] classes to find out which additional options they support.

== License

This project is licensed under http://opensource.org/licenses/MIT/[MIT License].
Expand Down
16 changes: 12 additions & 4 deletions lib/asciidoctor/rouge/docinfo_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ class DocinfoProcessor < ::Asciidoctor::Extensions::DocinfoProcessor
# @return [String, nil]
def process(document)
return unless document.attr?('source-highlighter', 'rouge')
return unless document.attr('rouge-css', 'class') == 'class'
style = document.attr('rouge-css', 'class')

if (theme = ::Rouge::Theme.find(document.attr('rouge-theme', DEFAULT_THEME)))
css = theme.render(scope: '.highlight')
['<style>', css, '</style>'].join("\n")
if (style == 'class')
if (theme = ::Rouge::Theme.find(document.attr('rouge-theme', DEFAULT_THEME)))
css = theme.render(scope: '.highlight')
['<style>', css, '</style>'].join("\n")
end
elsif (style == 'external')
if (theme = document.attr('rouge-theme'))
['<link rel="stylesheet" href="', theme, '">'].join("")
end
else
return
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor/rouge/treeprocessor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def process_listing(block)
# @param language [String]
# @return [Rouge::Lexer] a lexer for the specified *language*.
def find_lexer(language)
(::Rouge::Lexer.find(language) || ::Rouge::Lexers::PlainText).new
(::Rouge::Lexer.find_fancy(language) || ::Rouge::Lexers::PlainText.new)
end

# @param source [String] the code to highlight.
Expand Down