diff --git a/README.adoc b/README.adoc index c6cd5e7..35c35f4 100644 --- a/README.adoc +++ b/README.adoc @@ -1201,7 +1201,9 @@ You need to augment the layout to include resources typically present in a stand === Stylesheet for Code Highlighting -Asciidoctor integrates with Pygments to provide code highlighting of source blocks in AsciiDoc content. +Asciidoctor integrates with Pygments and Rouge to provide code highlighting of source blocks in AsciiDoc content. + +Rouge is the default highlighter for Jekyll versions 4.0 and later, so it is already installed with Jekyll. To enable Pygments, you must install the `pygments.rb` gem. To do so, add the `pygments.rb` gem to your [.path]_Gemfile_: @@ -1214,20 +1216,20 @@ gem 'pygments.rb', '~> 1.1.2' IMPORTANT: To use Pygments with Ruby >= 2.4 or JRuby, you must install pygments.rb >= 1.1.0. As part of this integration, Asciidoctor generates a custom stylesheet tailored specially to work with the HTML that Asciidocotor produces. -Since this stylesheet is backed by the Pygments API, it provides access to all the themes in Pygments +Since this stylesheet is backed by the Pygments API, it provides access to all the themes in Pygments. -This plugin will automatically generate a stylesheet for Pygments into the source directory if the AsciiDoc attributes in your site's {path-config} are configured as follows: +This plugin will automatically generate a stylesheet for Pygments or Rouge into the source directory if the AsciiDoc attributes in your site's {path-config} are configured as follows: -* `source-highlighter` has the value `pygments` -* `pygments-css` has the value `class` or is not set -* `pygments-stylesheet` is not unset (if set, it can have any value) +* `source-highlighter` has the value `pygments` or `rouge` +* `pygments-css` (or `rouge-css`, respectively) has the value `class` or is not set +* `pygments-stylesheet` (or `rouge-stylesheet`, respectively) is not unset (if set, it can have any value) -By default, the stylesheet is written to `stylesdir` + `pygments-stylesheet`. -If the `pygments-stylesheet` attribute is not specified, the value defaults to `asciidoc-pygments.css`. +By default, the stylesheet is written to `stylesdir` + `{pygments,rouge}-stylesheet`, depending on the value of `source-highlighter`. +If the `pygments-stylesheet` or `rouge-stylesheet` attribute is not specified, the value defaults to `asciidoc-{pygments,rouge}.css`. You can customize this value to your liking. -The Pygments theme is selected by the value of the `pygments-style` attribute. -If this attribute is not set, it defaults to `vs`. +The theme is selected by the value of the `pygments-style` or `rouge-style` attribute, respectively. +If this attribute is not set, it defaults to `vs` for Pygments and `github` for Rouge. The stylesheet file will be created if it does not yet exist or the theme has been changed. Jekyll will handle copying the file to the output directory. @@ -1239,7 +1241,16 @@ You'll need to add a line to your template to link to this stylesheet, such as: ---- -To disable this feature, either set the `pygments-css` to `style` (to enable inline styles) or unset the `pygments-stylesheet` attribute in your site's {path-config}. +for Pygments or + +[source,html] +---- + +---- + +or Rouge, respectively. + +To disable this feature, either set the `{pygments,rouge}-css` attribute to `style` (to enable inline styles) or unset the `pygments-stylesheet` and `rouge-stylesheet` attributes in your site's {path-config}. NOTE: It may still be necessary to make some tweaks to your site's stylesheet to accomodate this integration. diff --git a/lib/jekyll-asciidoc/integrator.rb b/lib/jekyll-asciidoc/integrator.rb index 661dad0..f71949f 100644 --- a/lib/jekyll-asciidoc/integrator.rb +++ b/lib/jekyll-asciidoc/integrator.rb @@ -1,11 +1,18 @@ module Jekyll module AsciiDoc # Promotes eligible AsciiDoc attributes to page variables and applies page-level settings to all documents handled - # by the converter included with this plugin. It also copies the custom Pygments stylesheet if Pygments is the - # source highlighter and configured to use class-based styling. + # by the converter included with this plugin. It also copies the custom Pygments or Rouge stylesheet if Pygments + # or Rouge is the source highlighter and configured to use class-based styling. class Integrator < ::Jekyll::Generator NewLine = Utils::NewLine - PygmentsRootSelector = /^(.+?)\.pygments +{/ + RootSelector = { + pygments: /^(.+?)\.pygments +{/, + rouge: /^(.+?)\.rouge +{/, + } + BaseStyles = { + pygments: 'vs', + rouge: 'github', + } # Enable plugin when running in safe mode; jekyll-asciidoc gem must also be declared in whitelist safe true @@ -36,9 +43,12 @@ def generate site if site.config['asciidoc']['processor'] == 'asciidoctor' attrs = site.config['asciidoctor'][:attributes] attrs['localdate'], attrs['localtime'] = (site.time.strftime '%Y-%m-%d %H:%M:%S %Z').split ' ', 2 - if ((attrs['source-highlighter'] || '').chomp '@') == 'pygments' && - ((attrs['pygments-css'] || '').chomp '@') != 'style' && (attrs.fetch 'pygments-stylesheet', '') - generate_pygments_stylesheet site, attrs + highlighter = (attrs['source-highlighter'] || '').chomp '@' + if %w(pygments rouge).include? highlighter + if ((attrs["#{highlighter}-css"] || '').chomp '@') != 'style' && + (attrs.fetch "#{highlighter}-stylesheet", '') + generate_stylesheet highlighter, site, attrs + end end end @@ -106,17 +116,17 @@ def integrate document, collection_name = nil data.fetch 'published', true end - def generate_pygments_stylesheet site, attrs + def generate_stylesheet highlighter, site, attrs css_base = site.source unless (css_dir = (attrs['stylesdir'] || '').chomp '@').empty? || (css_dir.start_with? '/') css_dir = %(/#{css_dir}) end - css_name = attrs['pygments-stylesheet'] || 'asciidoc-pygments.css' + css_name = attrs["#{highlighter}-stylesheet"] || "asciidoc-#{highlighter}.css" css_file = ::File.join css_base, css_dir, css_name - css_style = (attrs['pygments-style'] || 'vs').chomp '@' - css = ::Asciidoctor::Stylesheets.instance.pygments_stylesheet_data css_style + css_style = (attrs["#{highlighter}-style"] || BaseStyles[highlighter.to_sym]).chomp '@' + css = (::Asciidoctor::SyntaxHighlighter.for highlighter).read_stylesheet css_style # NOTE apply stronger CSS rule for general text color - css = css.sub PygmentsRootSelector, '\1.pygments, \1.pygments code {' + css = css.sub RootSelector[highlighter.to_sym], "\1.#{highlighter}, \1.#{highlighter} code {" if site.static_files.any? {|f| f.path == css_file } ::File.write css_file, css unless css == (::File.read css_file) else diff --git a/spec/fixtures/rouge_code_highlighting/_config.yml b/spec/fixtures/rouge_code_highlighting/_config.yml new file mode 100644 index 0000000..eff8bda --- /dev/null +++ b/spec/fixtures/rouge_code_highlighting/_config.yml @@ -0,0 +1,6 @@ +plugins: +- jekyll-asciidoc +asciidoctor: + attributes: + - stylesdir=/css + - source-highlighter=rouge@ diff --git a/spec/fixtures/rouge_code_highlighting/_layouts/page.html b/spec/fixtures/rouge_code_highlighting/_layouts/page.html new file mode 100644 index 0000000..5cf6b95 --- /dev/null +++ b/spec/fixtures/rouge_code_highlighting/_layouts/page.html @@ -0,0 +1,13 @@ + + +
+ +