nanoc-code-classifier 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +53 -0
- data/lib/nanoc-code-classifier.rb +1 -1
- data/lib/nanoc3/filters/code_classifier.rb +47 -9
- data/lib/nanoc3/filters/code_classifier/version.rb +1 -1
- metadata +23 -7
data/README.md
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
# nanoc-code-classifier
|
2
|
+
|
3
|
+
The nanoc syntax highlighting filter (Nanoc3::Filters::ColorizeSyntax) requires that the class of the `code` element is set to the language used in the `code` element. However it is impossible to set a class in markup formats other than HTML (e.g. markdown, textile, etc).
|
4
|
+
|
5
|
+
This nanoc filter pre-processes HTML for `code` elements for simple tags which are used to modify the markup for your code blocks.
|
6
|
+
|
7
|
+
For example, the following markdown:
|
8
|
+
|
9
|
+
This is a code block in ruby:
|
10
|
+
|
11
|
+
[@language="ruby"]
|
12
|
+
[@caption="Listing 1: Example in ruby."]
|
13
|
+
|
14
|
+
puts "hello world"
|
15
|
+
|
16
|
+
And this is a code block in javascript:
|
17
|
+
|
18
|
+
[@language="javascript"]
|
19
|
+
[@caption="Listing 2: Example in javascript."]
|
20
|
+
|
21
|
+
alert("hello world");
|
22
|
+
|
23
|
+
becomes:
|
24
|
+
|
25
|
+
<p>This is a code block in ruby:</p>
|
26
|
+
|
27
|
+
<figure>
|
28
|
+
<pre><code class="language-ruby">
|
29
|
+
puts "hello world"
|
30
|
+
</code></pre>
|
31
|
+
<figcaption>Listing 1: Example in ruby.</figcaption>
|
32
|
+
</figure>
|
33
|
+
|
34
|
+
<p>And this is a code block in javascript:</p>
|
35
|
+
|
36
|
+
<figure>
|
37
|
+
<pre><code class="language-javascript">
|
38
|
+
alert("hello world");
|
39
|
+
</code></pre>
|
40
|
+
<figcaption>Listing 2: Example in javascript.</figcaption>
|
41
|
+
</figure>
|
42
|
+
|
43
|
+
|
44
|
+
## Quickstart
|
45
|
+
|
46
|
+
This rule first generates HTML from markdown using bluecloth, then applies the language classes using nanoc-code-classifier and finally highlights the syntax using coderay.
|
47
|
+
|
48
|
+
compile "/*/" do
|
49
|
+
filter :bluecloth
|
50
|
+
filter :code_classifier, :pre_class => "coderay"
|
51
|
+
filter :colorize_syntax, :colorizers => {:ruby => :coderay}
|
52
|
+
layout "default"
|
53
|
+
end
|
@@ -1 +1 @@
|
|
1
|
-
require
|
1
|
+
require 'nanoc3/filters/code_classifier'
|
@@ -2,21 +2,59 @@ require 'nokogiri'
|
|
2
2
|
|
3
3
|
module Nanoc3
|
4
4
|
module Filters
|
5
|
-
# A nanoc filter which pre-processes
|
5
|
+
# A nanoc filter which pre-processes code elements for special tags.
|
6
|
+
#
|
7
|
+
# For example:
|
8
|
+
#
|
9
|
+
# [@language="ruby"]
|
10
|
+
# [@caption="lorem ipsum"]
|
6
11
|
class CodeClassifier < Nanoc3::Filter
|
7
12
|
identifier :code_classifier
|
8
13
|
type :text
|
9
14
|
|
10
15
|
def run(content, params = {})
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
html = Nokogiri::HTML.fragment(content)
|
17
|
+
|
18
|
+
html.xpath("pre/code").each do |code|
|
19
|
+
pre = code.parent
|
20
|
+
|
21
|
+
# Set the class on the <pre>.
|
22
|
+
append_class(pre, params[:pre_class]) if params[:pre_class]
|
23
|
+
|
24
|
+
process_language_tag(code)
|
25
|
+
process_caption_tag(code)
|
18
26
|
end
|
19
|
-
|
27
|
+
|
28
|
+
html.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def process_language_tag(element)
|
34
|
+
element.content = element.content.sub(/\[\s*@language\s*=\s*"([^"]+)"\s*\]/) do
|
35
|
+
append_class(element, "language-#{$1}")
|
36
|
+
nil
|
37
|
+
end.strip!
|
38
|
+
end
|
39
|
+
|
40
|
+
def process_caption_tag(element)
|
41
|
+
element.content = element.content.sub(/\[\s*@caption\s*=\s*"([^"]+)"\s*\]/) do
|
42
|
+
# Wrap in a <figure>.
|
43
|
+
figure = Nokogiri::XML::Node.new("figure", element.document)
|
44
|
+
element.parent.add_next_sibling(figure)
|
45
|
+
element.parent.parent = figure
|
46
|
+
|
47
|
+
# Add a <figcaption>.
|
48
|
+
figcaption = Nokogiri::XML::Node.new("figcaption", element.document)
|
49
|
+
figcaption.content = $1
|
50
|
+
element.parent.add_next_sibling(figcaption)
|
51
|
+
|
52
|
+
nil
|
53
|
+
end.strip!
|
54
|
+
end
|
55
|
+
|
56
|
+
def append_class(element, klass)
|
57
|
+
element["class"] = ((element["class"] || "") + " " + klass).strip
|
20
58
|
end
|
21
59
|
end
|
22
60
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nanoc-code-classifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Josh Bassett
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-21 00:00:00 +11:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -24,14 +24,30 @@ dependencies:
|
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
26
26
|
requirements:
|
27
|
-
- -
|
27
|
+
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 15
|
30
30
|
segments:
|
31
|
+
- 1
|
31
32
|
- 0
|
32
|
-
version: "0"
|
33
|
+
version: "1.0"
|
33
34
|
type: :runtime
|
34
35
|
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: nanoc
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 7
|
45
|
+
segments:
|
46
|
+
- 3
|
47
|
+
- 0
|
48
|
+
version: "3.0"
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
35
51
|
description: By using special tags the code classifier filter can pre-process you code blocks and apply classes to the generated HTML.
|
36
52
|
email: josh.bassett@gmail.com
|
37
53
|
executables: []
|