|
| 1 | +# Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 | +# for details. All rights reserved. Use of this source code is governed by a |
| 3 | +# BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +require 'cgi' |
| 6 | + |
| 7 | +module Prettify |
| 8 | + |
| 9 | + # Wraps code with tags for Prettify. |
| 10 | + # |
| 11 | + # Example usage: |
| 12 | + # |
| 13 | + # {% prettify dart %} |
| 14 | + # // dart code here |
| 15 | + # {% endprettify %} |
| 16 | + # |
| 17 | + # The language name can be ommitted if it is not known. |
| 18 | + class Tag < Liquid::Block |
| 19 | + |
| 20 | + Syntax = /\s*(\w+)\s*/o |
| 21 | + |
| 22 | + def initialize(tag_name, markup, tokens) |
| 23 | + super |
| 24 | + if markup =~ Syntax |
| 25 | + @lang = $1 |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + def render(context) |
| 30 | + # out = '<pre class="prettyprint linenums' |
| 31 | + out = '<pre class="prettyprint' |
| 32 | + unless @lang.nil? |
| 33 | + out += ' lang-' + @lang |
| 34 | + end |
| 35 | + out += '">' |
| 36 | + |
| 37 | + contents = super #.strip |
| 38 | + contents = CGI::escapeHTML(contents) |
| 39 | + |
| 40 | + contents.gsub!('[[strike]]', '<code class="nocode strike">') |
| 41 | + contents.gsub!('[[/strike]]', '</code>') |
| 42 | + |
| 43 | + contents.gsub!('[[highlight]]', '<code class="nocode highlight">') |
| 44 | + contents.gsub!('[[/highlight]]', '</code>') |
| 45 | + |
| 46 | + contents.gsub!('[[note]]', '<code class="nocode note">') |
| 47 | + contents.gsub!('[[/note]]', '</code>') |
| 48 | + |
| 49 | + contents.gsub!('[[red]]', '<code class="nocode red">') |
| 50 | + contents.gsub!('[[/red]]', '</code>') |
| 51 | + |
| 52 | + out += contents + "</pre>" |
| 53 | + end |
| 54 | + |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +Liquid::Template.register_tag('prettify', Prettify::Tag) |
0 commit comments