From 6f0da2041c33632487a39c01b348bd6db35cd759 Mon Sep 17 00:00:00 2001 From: simonis Date: Mon, 17 Dec 2018 19:31:29 +0100 Subject: [PATCH 1/2] Allow linking to external style sheet --- README.adoc | 3 ++- lib/asciidoctor/rouge/docinfo_processor.rb | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.adoc b/README.adoc index dc13989..4d7c2ae 100644 --- a/README.adoc +++ b/README.adoc @@ -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:: diff --git a/lib/asciidoctor/rouge/docinfo_processor.rb b/lib/asciidoctor/rouge/docinfo_processor.rb index 799caaa..2da792f 100644 --- a/lib/asciidoctor/rouge/docinfo_processor.rb +++ b/lib/asciidoctor/rouge/docinfo_processor.rb @@ -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') - [''].join("\n") + if (style == 'class') + if (theme = ::Rouge::Theme.find(document.attr('rouge-theme', DEFAULT_THEME))) + css = theme.render(scope: '.highlight') + [''].join("\n") + end + elsif (style == 'external') + if (theme = document.attr('rouge-theme')) + [''].join("") + end + else + return end end end From 69bcf1f0b2e15f64b1f51a810000bfe8f9c54859 Mon Sep 17 00:00:00 2001 From: simonis Date: Sat, 29 Dec 2018 15:25:04 +0100 Subject: [PATCH 2/2] Use Lexer.find_fancy() for language selection to allow passing CGI-style options to the Lexer --- README.adoc | 16 ++++++++++++++++ lib/asciidoctor/rouge/treeprocessor.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index 4d7c2ae..0f5dbec 100644 --- a/README.adoc +++ b/README.adoc @@ -64,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]. diff --git a/lib/asciidoctor/rouge/treeprocessor.rb b/lib/asciidoctor/rouge/treeprocessor.rb index cd981d9..19499e0 100644 --- a/lib/asciidoctor/rouge/treeprocessor.rb +++ b/lib/asciidoctor/rouge/treeprocessor.rb @@ -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.