nanoc3 3.2.4 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. metadata +14 -313
  2. data/.gemtest +0 -0
  3. data/ChangeLog +0 -3
  4. data/Gemfile +0 -29
  5. data/LICENSE +0 -19
  6. data/NEWS.md +0 -449
  7. data/README.md +0 -108
  8. data/Rakefile +0 -14
  9. data/bin/nanoc3 +0 -12
  10. data/doc/yardoc_templates/default/layout/html/footer.erb +0 -10
  11. data/lib/nanoc3.rb +0 -28
  12. data/lib/nanoc3/base.rb +0 -49
  13. data/lib/nanoc3/base/compilation/checksum_store.rb +0 -57
  14. data/lib/nanoc3/base/compilation/compiled_content_cache.rb +0 -62
  15. data/lib/nanoc3/base/compilation/compiler.rb +0 -455
  16. data/lib/nanoc3/base/compilation/compiler_dsl.rb +0 -214
  17. data/lib/nanoc3/base/compilation/dependency_tracker.rb +0 -196
  18. data/lib/nanoc3/base/compilation/filter.rb +0 -165
  19. data/lib/nanoc3/base/compilation/item_rep_proxy.rb +0 -102
  20. data/lib/nanoc3/base/compilation/item_rep_recorder_proxy.rb +0 -88
  21. data/lib/nanoc3/base/compilation/outdatedness_checker.rb +0 -223
  22. data/lib/nanoc3/base/compilation/outdatedness_reasons.rb +0 -46
  23. data/lib/nanoc3/base/compilation/rule.rb +0 -73
  24. data/lib/nanoc3/base/compilation/rule_context.rb +0 -84
  25. data/lib/nanoc3/base/compilation/rule_memory_calculator.rb +0 -40
  26. data/lib/nanoc3/base/compilation/rule_memory_store.rb +0 -53
  27. data/lib/nanoc3/base/compilation/rules_collection.rb +0 -231
  28. data/lib/nanoc3/base/context.rb +0 -47
  29. data/lib/nanoc3/base/core_ext.rb +0 -6
  30. data/lib/nanoc3/base/core_ext/array.rb +0 -62
  31. data/lib/nanoc3/base/core_ext/hash.rb +0 -63
  32. data/lib/nanoc3/base/core_ext/pathname.rb +0 -26
  33. data/lib/nanoc3/base/core_ext/string.rb +0 -46
  34. data/lib/nanoc3/base/directed_graph.rb +0 -275
  35. data/lib/nanoc3/base/errors.rb +0 -174
  36. data/lib/nanoc3/base/memoization.rb +0 -67
  37. data/lib/nanoc3/base/notification_center.rb +0 -84
  38. data/lib/nanoc3/base/ordered_hash.rb +0 -200
  39. data/lib/nanoc3/base/plugin_registry.rb +0 -165
  40. data/lib/nanoc3/base/result_data/item_rep.rb +0 -488
  41. data/lib/nanoc3/base/source_data/code_snippet.rb +0 -58
  42. data/lib/nanoc3/base/source_data/configuration.rb +0 -24
  43. data/lib/nanoc3/base/source_data/data_source.rb +0 -234
  44. data/lib/nanoc3/base/source_data/item.rb +0 -301
  45. data/lib/nanoc3/base/source_data/layout.rb +0 -130
  46. data/lib/nanoc3/base/source_data/site.rb +0 -361
  47. data/lib/nanoc3/base/store.rb +0 -135
  48. data/lib/nanoc3/cli.rb +0 -133
  49. data/lib/nanoc3/cli/command.rb +0 -139
  50. data/lib/nanoc3/cli/commands/autocompile.rb +0 -60
  51. data/lib/nanoc3/cli/commands/compile.rb +0 -280
  52. data/lib/nanoc3/cli/commands/create_item.rb +0 -62
  53. data/lib/nanoc3/cli/commands/create_layout.rb +0 -75
  54. data/lib/nanoc3/cli/commands/create_site.rb +0 -410
  55. data/lib/nanoc3/cli/commands/debug.rb +0 -119
  56. data/lib/nanoc3/cli/commands/info.rb +0 -98
  57. data/lib/nanoc3/cli/commands/nanoc.rb +0 -37
  58. data/lib/nanoc3/cli/commands/update.rb +0 -72
  59. data/lib/nanoc3/cli/commands/view.rb +0 -84
  60. data/lib/nanoc3/cli/commands/watch.rb +0 -125
  61. data/lib/nanoc3/cli/error_handler.rb +0 -193
  62. data/lib/nanoc3/cli/logger.rb +0 -91
  63. data/lib/nanoc3/data_sources.rb +0 -29
  64. data/lib/nanoc3/data_sources/deprecated/delicious.rb +0 -42
  65. data/lib/nanoc3/data_sources/deprecated/last_fm.rb +0 -87
  66. data/lib/nanoc3/data_sources/deprecated/twitter.rb +0 -38
  67. data/lib/nanoc3/data_sources/filesystem.rb +0 -299
  68. data/lib/nanoc3/data_sources/filesystem_unified.rb +0 -116
  69. data/lib/nanoc3/data_sources/filesystem_verbose.rb +0 -86
  70. data/lib/nanoc3/extra.rb +0 -22
  71. data/lib/nanoc3/extra/auto_compiler.rb +0 -103
  72. data/lib/nanoc3/extra/chick.rb +0 -125
  73. data/lib/nanoc3/extra/core_ext.rb +0 -4
  74. data/lib/nanoc3/extra/core_ext/enumerable.rb +0 -33
  75. data/lib/nanoc3/extra/core_ext/time.rb +0 -19
  76. data/lib/nanoc3/extra/deployers.rb +0 -11
  77. data/lib/nanoc3/extra/deployers/rsync.rb +0 -114
  78. data/lib/nanoc3/extra/file_proxy.rb +0 -40
  79. data/lib/nanoc3/extra/validators.rb +0 -12
  80. data/lib/nanoc3/extra/validators/links.rb +0 -264
  81. data/lib/nanoc3/extra/validators/w3c.rb +0 -95
  82. data/lib/nanoc3/extra/vcs.rb +0 -66
  83. data/lib/nanoc3/extra/vcses.rb +0 -17
  84. data/lib/nanoc3/extra/vcses/bazaar.rb +0 -25
  85. data/lib/nanoc3/extra/vcses/dummy.rb +0 -24
  86. data/lib/nanoc3/extra/vcses/git.rb +0 -25
  87. data/lib/nanoc3/extra/vcses/mercurial.rb +0 -25
  88. data/lib/nanoc3/extra/vcses/subversion.rb +0 -25
  89. data/lib/nanoc3/filters.rb +0 -53
  90. data/lib/nanoc3/filters/asciidoc.rb +0 -38
  91. data/lib/nanoc3/filters/bluecloth.rb +0 -19
  92. data/lib/nanoc3/filters/coderay.rb +0 -21
  93. data/lib/nanoc3/filters/colorize_syntax.rb +0 -261
  94. data/lib/nanoc3/filters/erb.rb +0 -35
  95. data/lib/nanoc3/filters/erubis.rb +0 -27
  96. data/lib/nanoc3/filters/haml.rb +0 -27
  97. data/lib/nanoc3/filters/kramdown.rb +0 -20
  98. data/lib/nanoc3/filters/less.rb +0 -53
  99. data/lib/nanoc3/filters/markaby.rb +0 -20
  100. data/lib/nanoc3/filters/maruku.rb +0 -20
  101. data/lib/nanoc3/filters/mustache.rb +0 -24
  102. data/lib/nanoc3/filters/rainpress.rb +0 -19
  103. data/lib/nanoc3/filters/rdiscount.rb +0 -22
  104. data/lib/nanoc3/filters/rdoc.rb +0 -33
  105. data/lib/nanoc3/filters/redcarpet.rb +0 -27
  106. data/lib/nanoc3/filters/redcloth.rb +0 -47
  107. data/lib/nanoc3/filters/relativize_paths.rb +0 -45
  108. data/lib/nanoc3/filters/rubypants.rb +0 -20
  109. data/lib/nanoc3/filters/sass.rb +0 -66
  110. data/lib/nanoc3/filters/slim.rb +0 -25
  111. data/lib/nanoc3/filters/typogruby.rb +0 -23
  112. data/lib/nanoc3/filters/uglify_js.rb +0 -42
  113. data/lib/nanoc3/helpers.rb +0 -16
  114. data/lib/nanoc3/helpers/blogging.rb +0 -319
  115. data/lib/nanoc3/helpers/breadcrumbs.rb +0 -40
  116. data/lib/nanoc3/helpers/capturing.rb +0 -138
  117. data/lib/nanoc3/helpers/filtering.rb +0 -50
  118. data/lib/nanoc3/helpers/html_escape.rb +0 -55
  119. data/lib/nanoc3/helpers/link_to.rb +0 -151
  120. data/lib/nanoc3/helpers/rendering.rb +0 -140
  121. data/lib/nanoc3/helpers/tagging.rb +0 -71
  122. data/lib/nanoc3/helpers/text.rb +0 -44
  123. data/lib/nanoc3/helpers/xml_sitemap.rb +0 -76
  124. data/lib/nanoc3/tasks.rb +0 -10
  125. data/lib/nanoc3/tasks/clean.rake +0 -16
  126. data/lib/nanoc3/tasks/clean.rb +0 -29
  127. data/lib/nanoc3/tasks/deploy/rsync.rake +0 -14
  128. data/lib/nanoc3/tasks/validate.rake +0 -92
  129. data/nanoc3.gemspec +0 -49
  130. data/tasks/doc.rake +0 -16
  131. data/tasks/test.rake +0 -44
  132. data/test/base/core_ext/array_spec.rb +0 -73
  133. data/test/base/core_ext/hash_spec.rb +0 -98
  134. data/test/base/core_ext/pathname_spec.rb +0 -27
  135. data/test/base/core_ext/string_spec.rb +0 -37
  136. data/test/base/test_checksum_store.rb +0 -35
  137. data/test/base/test_code_snippet.rb +0 -31
  138. data/test/base/test_compiler.rb +0 -316
  139. data/test/base/test_compiler_dsl.rb +0 -161
  140. data/test/base/test_context.rb +0 -31
  141. data/test/base/test_data_source.rb +0 -46
  142. data/test/base/test_dependency_tracker.rb +0 -262
  143. data/test/base/test_directed_graph.rb +0 -283
  144. data/test/base/test_filter.rb +0 -83
  145. data/test/base/test_item.rb +0 -179
  146. data/test/base/test_item_rep.rb +0 -553
  147. data/test/base/test_layout.rb +0 -59
  148. data/test/base/test_memoization.rb +0 -90
  149. data/test/base/test_notification_center.rb +0 -34
  150. data/test/base/test_outdatedness_checker.rb +0 -394
  151. data/test/base/test_plugin.rb +0 -30
  152. data/test/base/test_rule.rb +0 -19
  153. data/test/base/test_rule_context.rb +0 -65
  154. data/test/base/test_site.rb +0 -190
  155. data/test/cli/commands/test_compile.rb +0 -33
  156. data/test/cli/commands/test_create_item.rb +0 -14
  157. data/test/cli/commands/test_create_layout.rb +0 -28
  158. data/test/cli/commands/test_create_site.rb +0 -24
  159. data/test/cli/commands/test_help.rb +0 -12
  160. data/test/cli/commands/test_info.rb +0 -11
  161. data/test/cli/commands/test_update.rb +0 -10
  162. data/test/cli/test_cli.rb +0 -102
  163. data/test/cli/test_error_handler.rb +0 -29
  164. data/test/cli/test_logger.rb +0 -10
  165. data/test/data_sources/test_filesystem.rb +0 -433
  166. data/test/data_sources/test_filesystem_unified.rb +0 -536
  167. data/test/data_sources/test_filesystem_verbose.rb +0 -357
  168. data/test/extra/core_ext/test_enumerable.rb +0 -30
  169. data/test/extra/core_ext/test_time.rb +0 -15
  170. data/test/extra/deployers/test_rsync.rb +0 -232
  171. data/test/extra/test_auto_compiler.rb +0 -417
  172. data/test/extra/test_file_proxy.rb +0 -19
  173. data/test/extra/test_vcs.rb +0 -22
  174. data/test/extra/validators/test_links.rb +0 -51
  175. data/test/extra/validators/test_w3c.rb +0 -47
  176. data/test/filters/test_asciidoc.rb +0 -22
  177. data/test/filters/test_bluecloth.rb +0 -18
  178. data/test/filters/test_coderay.rb +0 -44
  179. data/test/filters/test_colorize_syntax.rb +0 -283
  180. data/test/filters/test_erb.rb +0 -99
  181. data/test/filters/test_erubis.rb +0 -70
  182. data/test/filters/test_haml.rb +0 -96
  183. data/test/filters/test_kramdown.rb +0 -18
  184. data/test/filters/test_less.rb +0 -113
  185. data/test/filters/test_markaby.rb +0 -24
  186. data/test/filters/test_maruku.rb +0 -18
  187. data/test/filters/test_mustache.rb +0 -25
  188. data/test/filters/test_rainpress.rb +0 -29
  189. data/test/filters/test_rdiscount.rb +0 -31
  190. data/test/filters/test_rdoc.rb +0 -18
  191. data/test/filters/test_redcarpet.rb +0 -63
  192. data/test/filters/test_redcloth.rb +0 -33
  193. data/test/filters/test_relativize_paths.rb +0 -332
  194. data/test/filters/test_rubypants.rb +0 -18
  195. data/test/filters/test_sass.rb +0 -229
  196. data/test/filters/test_slim.rb +0 -35
  197. data/test/filters/test_typogruby.rb +0 -21
  198. data/test/filters/test_uglify_js.rb +0 -30
  199. data/test/gem_loader.rb +0 -11
  200. data/test/helper.rb +0 -179
  201. data/test/helpers/test_blogging.rb +0 -754
  202. data/test/helpers/test_breadcrumbs.rb +0 -81
  203. data/test/helpers/test_capturing.rb +0 -41
  204. data/test/helpers/test_filtering.rb +0 -106
  205. data/test/helpers/test_html_escape.rb +0 -32
  206. data/test/helpers/test_link_to.rb +0 -249
  207. data/test/helpers/test_rendering.rb +0 -89
  208. data/test/helpers/test_tagging.rb +0 -87
  209. data/test/helpers/test_text.rb +0 -24
  210. data/test/helpers/test_xml_sitemap.rb +0 -103
  211. data/test/tasks/test_clean.rb +0 -67
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc3::Extra::VCSes
4
-
5
- # @see Nanoc3::Extra::VCS
6
- class Bazaar < Nanoc3::Extra::VCS
7
-
8
- # @see Nanoc3::Extra::VCS#add
9
- def add(filename)
10
- system('bzr', 'add', filename)
11
- end
12
-
13
- # @see Nanoc3::Extra::VCS#remove
14
- def remove(filename)
15
- system('bzr', 'rm', filename)
16
- end
17
-
18
- # @see Nanoc3::Extra::VCS#move
19
- def move(src, dst)
20
- system('bzr', 'mv', src, dst)
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,24 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc3::Extra::VCSes
4
-
5
- # @see Nanoc3::Extra::VCS
6
- class Dummy < Nanoc3::Extra::VCS
7
-
8
- # @see Nanoc3::Extra::VCS#add
9
- def add(filename)
10
- end
11
-
12
- # @see Nanoc3::Extra::VCS#remove
13
- def remove(filename)
14
- FileUtils.rm_rf(filename)
15
- end
16
-
17
- # @see Nanoc3::Extra::VCS#move
18
- def move(src, dst)
19
- FileUtils.move(src, dst)
20
- end
21
-
22
- end
23
-
24
- end
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc3::Extra::VCSes
4
-
5
- # @see Nanoc3::Extra::VCS
6
- class Git < Nanoc3::Extra::VCS
7
-
8
- # @see Nanoc3::Extra::VCS#add
9
- def add(filename)
10
- system('git', 'add', filename)
11
- end
12
-
13
- # @see Nanoc3::Extra::VCS#remove
14
- def remove(filename)
15
- system('git', 'rm', filename)
16
- end
17
-
18
- # @see Nanoc3::Extra::VCS#move
19
- def move(src, dst)
20
- system('git', 'mv', src, dst)
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc3::Extra::VCSes
4
-
5
- # @see Nanoc3::Extra::VCS
6
- class Mercurial < Nanoc3::Extra::VCS
7
-
8
- # @see Nanoc3::Extra::VCS#add
9
- def add(filename)
10
- system('hg', 'add', filename)
11
- end
12
-
13
- # @see Nanoc3::Extra::VCS#remove
14
- def remove(filename)
15
- system('hg', 'rm', filename)
16
- end
17
-
18
- # @see Nanoc3::Extra::VCS#move
19
- def move(src, dst)
20
- system('hg', 'mv', src, dst)
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,25 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc3::Extra::VCSes
4
-
5
- # @see Nanoc3::Extra::VCS
6
- class Subversion < Nanoc3::Extra::VCS
7
-
8
- # @see Nanoc3::Extra::VCS#add
9
- def add(filename)
10
- system('svn', 'add', filename)
11
- end
12
-
13
- # @see Nanoc3::Extra::VCS#remove
14
- def remove(filename)
15
- system('svn', 'rm', filename)
16
- end
17
-
18
- # @see Nanoc3::Extra::VCS#move
19
- def move(src, dst)
20
- system('svn', 'mv', src, dst)
21
- end
22
-
23
- end
24
-
25
- end
@@ -1,53 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc3::Filters
4
-
5
- autoload 'AsciiDoc', 'nanoc3/filters/asciidoc'
6
- autoload 'BlueCloth', 'nanoc3/filters/bluecloth'
7
- autoload 'CodeRay', 'nanoc3/filters/coderay'
8
- autoload 'ColorizeSyntax', 'nanoc3/filters/colorize_syntax'
9
- autoload 'ERB', 'nanoc3/filters/erb'
10
- autoload 'Erubis', 'nanoc3/filters/erubis'
11
- autoload 'Haml', 'nanoc3/filters/haml'
12
- autoload 'Kramdown', 'nanoc3/filters/kramdown'
13
- autoload 'Less', 'nanoc3/filters/less'
14
- autoload 'Markaby', 'nanoc3/filters/markaby'
15
- autoload 'Maruku', 'nanoc3/filters/maruku'
16
- autoload 'Mustache', 'nanoc3/filters/mustache'
17
- autoload 'Rainpress', 'nanoc3/filters/rainpress'
18
- autoload 'RDiscount', 'nanoc3/filters/rdiscount'
19
- autoload 'RDoc', 'nanoc3/filters/rdoc'
20
- autoload 'Redcarpet', 'nanoc3/filters/redcarpet'
21
- autoload 'RedCloth', 'nanoc3/filters/redcloth'
22
- autoload 'RelativizePaths', 'nanoc3/filters/relativize_paths'
23
- autoload 'RubyPants', 'nanoc3/filters/rubypants'
24
- autoload 'Sass', 'nanoc3/filters/sass'
25
- autoload 'Slim', 'nanoc3/filters/slim'
26
- autoload 'Typogruby', 'nanoc3/filters/typogruby'
27
- autoload 'UglifyJS', 'nanoc3/filters/uglify_js'
28
-
29
- Nanoc3::Filter.register '::Nanoc3::Filters::AsciiDoc', :asciidoc
30
- Nanoc3::Filter.register '::Nanoc3::Filters::BlueCloth', :bluecloth
31
- Nanoc3::Filter.register '::Nanoc3::Filters::CodeRay', :coderay
32
- Nanoc3::Filter.register '::Nanoc3::Filters::ColorizeSyntax', :colorize_syntax
33
- Nanoc3::Filter.register '::Nanoc3::Filters::ERB', :erb
34
- Nanoc3::Filter.register '::Nanoc3::Filters::Erubis', :erubis
35
- Nanoc3::Filter.register '::Nanoc3::Filters::Haml', :haml
36
- Nanoc3::Filter.register '::Nanoc3::Filters::Kramdown', :kramdown
37
- Nanoc3::Filter.register '::Nanoc3::Filters::Less', :less
38
- Nanoc3::Filter.register '::Nanoc3::Filters::Markaby', :markaby
39
- Nanoc3::Filter.register '::Nanoc3::Filters::Maruku', :maruku
40
- Nanoc3::Filter.register '::Nanoc3::Filters::Mustache', :mustache
41
- Nanoc3::Filter.register '::Nanoc3::Filters::Rainpress', :rainpress
42
- Nanoc3::Filter.register '::Nanoc3::Filters::RDiscount', :rdiscount
43
- Nanoc3::Filter.register '::Nanoc3::Filters::RDoc', :rdoc
44
- Nanoc3::Filter.register '::Nanoc3::Filters::Redcarpet', :redcarpet
45
- Nanoc3::Filter.register '::Nanoc3::Filters::RedCloth', :redcloth
46
- Nanoc3::Filter.register '::Nanoc3::Filters::RelativizePaths', :relativize_paths
47
- Nanoc3::Filter.register '::Nanoc3::Filters::RubyPants', :rubypants
48
- Nanoc3::Filter.register '::Nanoc3::Filters::Sass', :sass
49
- Nanoc3::Filter.register '::Nanoc3::Filters::Slim', :slim
50
- Nanoc3::Filter.register '::Nanoc3::Filters::Typogruby', :typogruby
51
- Nanoc3::Filter.register '::Nanoc3::Filters::UglifyJS', :uglify_js
52
-
53
- end
@@ -1,38 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'systemu'
4
-
5
- module Nanoc3::Filters
6
-
7
- # @since 3.2.0
8
- class AsciiDoc < Nanoc3::Filter
9
-
10
- # Runs the content through [AsciiDoc](http://www.methods.co.nz/asciidoc/).
11
- # This method takes no options.
12
- #
13
- # @param [String] content The content to filter
14
- #
15
- # @return [String] The filtered content
16
- def run(content, params={})
17
- # Run command
18
- stdout = ''
19
- stderr = ''
20
- status = systemu(
21
- [ 'asciidoc', '-o', '-', '-' ],
22
- 'stdin' => content,
23
- 'stdout' => stdout,
24
- 'stderr' => stderr)
25
-
26
- # Show errors
27
- unless status.success?
28
- $stderr.puts stderr
29
- raise RuntimeError, "AsciiDoc filter failed with status #{status}"
30
- end
31
-
32
- # Get result
33
- stdout
34
- end
35
-
36
- end
37
-
38
- end
@@ -1,19 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'bluecloth'
4
-
5
- module Nanoc3::Filters
6
- class BlueCloth < Nanoc3::Filter
7
-
8
- # Runs the content through [BlueCloth](http://deveiate.org/projects/BlueCloth).
9
- # This method takes no options.
10
- #
11
- # @param [String] content The content to filter
12
- #
13
- # @return [String] The filtered content
14
- def run(content, params={})
15
- ::BlueCloth.new(content).to_html
16
- end
17
-
18
- end
19
- end
@@ -1,21 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'coderay'
4
-
5
- module Nanoc3::Filters
6
- class CodeRay < Nanoc3::Filter
7
-
8
- # @deprecated Use the `:colorize_syntax` filter instead.
9
- def run(content, params={})
10
- # Warn
11
- warn 'The :coderay filter is deprecated; consider using the :colorize_syntax filter instead.'
12
-
13
- # Check params
14
- raise ArgumentError, "CodeRay filter requires a :language argument which is missing" if params[:language].nil?
15
-
16
- # Get result
17
- ::CodeRay.scan(content, params[:language]).html
18
- end
19
-
20
- end
21
- end
@@ -1,261 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'nokogiri'
4
- require 'stringio'
5
- require 'open3'
6
-
7
- module Nanoc3::Filters
8
- class ColorizeSyntax < Nanoc3::Filter
9
-
10
- # The default colorizer to use for a language if the colorizer for that
11
- # language is not overridden.
12
- DEFAULT_COLORIZER = :coderay
13
-
14
- # Syntax-highlights code blocks in the given content. Code blocks should
15
- # be enclosed in `pre` elements that contain a `code` element. The code
16
- # element should have an indication of the language the code is in. There
17
- # are two possible ways of adding such an indication:
18
- #
19
- # 1. A HTML class starting with `language-` and followed by the
20
- # code language, as specified by HTML5. For example, `<code class="language-ruby">`.
21
- #
22
- # 2. A comment on the very first line of the code block in the format
23
- # `#!language` where `language` is the language the code is in. For
24
- # example, `#!ruby`.
25
- #
26
- # Options for individual colorizers will be taken from the {#run}
27
- # options’ value for the given colorizer. For example, if the filter is
28
- # invoked with a `:coderay => coderay_options_hash` option, the
29
- # `coderay_options_hash` hash will be passed to the CodeRay colorizer.
30
- #
31
- # Currently, only the `:coderay` (http://coderay.rubychan.de/),
32
- # `:pygmentize` (http://pygments.org/, http://pygments.org/docs/cmdline/),
33
- # and `:simon_highlight`
34
- # (http://www.andre-simon.de/doku/highlight/en/highlight.html) colorizers
35
- # are implemented. Additional colorizer implementations are welcome!
36
- #
37
- # @example Using a class to indicate type of code be highlighted
38
- #
39
- # <pre><code class="language-ruby">
40
- # def foo
41
- # "asdf"
42
- # end
43
- # </code></pre>
44
- #
45
- # @example Using a comment to indicate type of code be highlighted
46
- #
47
- # <pre><code>
48
- # #!ruby
49
- # def foo
50
- # "asdf"
51
- # end
52
- # </code></pre>
53
- #
54
- # @example Invoking the filter with custom parameters
55
- #
56
- # filter :colorize_syntax,
57
- # :colorizers => { :ruby => :coderay },
58
- # :coderay => { :line_numbers => :list }
59
- #
60
- # @param [String] content The content to filter
61
- #
62
- # @option params [Symbol] :default_colorizer (DEFAULT_COLORIZER) The
63
- # default colorizer, i.e. the colorizer that will be used when the
64
- # colorizer is not overriden for a specific language.
65
- #
66
- # @option params [Symbol] :syntax (:html) The syntax to use, which can be
67
- # `:html`, `:xml` or `:xhtml`, the latter two being the same.
68
- #
69
- # @option params [Hash] :colorizers ({}) A hash containing
70
- # a mapping of programming languages (symbols, not strings) onto
71
- # colorizers (symbols).
72
- #
73
- # @return [String] The filtered content
74
- def run(content, params={})
75
- # Take colorizers from parameters
76
- @colorizers = Hash.new(params[:default_colorizer] || DEFAULT_COLORIZER)
77
- (params[:colorizers] || {}).each_pair do |language, colorizer|
78
- @colorizers[language] = colorizer
79
- end
80
-
81
- # Determine syntax (HTML or XML)
82
- syntax = params[:syntax] || :html
83
- case syntax
84
- when :html
85
- klass = Nokogiri::HTML
86
- when :xml, :xhtml
87
- klass = Nokogiri::XML
88
- else
89
- raise RuntimeError, "unknown syntax: #{syntax.inspect} (expected :html or :xml)"
90
- end
91
-
92
- # Colorize
93
- doc = klass.fragment(content)
94
- doc.css('pre > code').each do |element|
95
- # Get language
96
- has_class = false
97
- language = nil
98
- if element['class']
99
- # Get language from class
100
- match = element['class'].match(/(^| )language-([^ ]+)/)
101
- language = match[2] if match
102
- has_class = true if language
103
- else
104
- # Get language from comment line
105
- match = element.inner_text.match(/^#!([^\/][^\n]*)$/)
106
- language = match[1] if match
107
- element.content = element.content.sub(/^#!([^\/][^\n]*)$\n/, '') if language
108
- end
109
-
110
- # Give up if there is no hope left
111
- next if language.nil?
112
-
113
- # Highlight
114
- raw = strip(element.inner_text)
115
- highlighted_code = highlight(raw, language, params)
116
- element.inner_html = Nokogiri::HTML.fragment(strip(highlighted_code), 'utf-8')
117
-
118
- # Add class
119
- unless has_class
120
- klass = element['class'] || ''
121
- klass << ' ' unless [' ', nil].include?(klass[-1,1])
122
- klass << "language-#{language}"
123
- element['class'] = klass
124
- end
125
- end
126
-
127
- method = "to_#{syntax}".to_sym
128
- doc.send(method, :encoding => 'UTF-8')
129
- end
130
-
131
- private
132
-
133
- KNOWN_COLORIZERS = [ :coderay, :dummy, :pygmentize, :simon_highlight ]
134
-
135
- # Removes the first blank lines and any whitespace at the end.
136
- def strip(s)
137
- s.lines.drop_while { |line| line.strip.empty? }.join.rstrip
138
- end
139
-
140
- def highlight(code, language, params={})
141
- colorizer = @colorizers[language.to_sym]
142
- if KNOWN_COLORIZERS.include?(colorizer)
143
- send(colorizer, code, language, params[colorizer] || {})
144
- else
145
- raise RuntimeError, "I don’t know how to highlight code using the “#{colorizer}” colorizer"
146
- end
147
- end
148
-
149
- # Runs the code through [CodeRay](http://coderay.rubychan.de/).
150
- #
151
- # @api private
152
- #
153
- # @param [String] code The code to colorize
154
- #
155
- # @param [String] language The language the code is written in
156
- #
157
- # @param [Hash] params Parameters to pass on to CodeRay
158
- #
159
- # @return [String] The colorized output
160
- def coderay(code, language, params={})
161
- require 'coderay'
162
-
163
- ::CodeRay.scan(code, language).html(params)
164
- end
165
-
166
- # Returns the input itself, not performing any code highlighting.
167
- #
168
- # @param [String] code The code to colorize
169
- #
170
- # @param [String] language The language the code is written in (unused)
171
- #
172
- # @return [String] The colorized output, which is identical to the input
173
- # in this case
174
- def dummy(code, language, params={})
175
- code
176
- end
177
-
178
- # Runs the content through [pygmentize](http://pygments.org/docs/cmdline/),
179
- # the commandline frontend for [Pygments](http://pygments.org/).
180
- #
181
- # @api private
182
- #
183
- # @param [String] code The code to colorize
184
- #
185
- # @param [String] language The language the code is written in
186
- #
187
- # @option params [String, Symbol] :encoding The encoding of the code block
188
- #
189
- # @return [String] The colorized output
190
- def pygmentize(code, language, params={})
191
- require 'systemu'
192
- check_availability('pygmentize', '-V')
193
-
194
- params[:encoding] ||= 'utf-8'
195
- params[:nowrap] ||= 'True'
196
-
197
- # Build command
198
- cmd = [ 'pygmentize', '-l', language, '-f', 'html' ]
199
- cmd << '-O' << params.map { |k,v| "#{k}=#{v}" }.join(',') unless params.empty?
200
-
201
- # Run command
202
- stdout = StringIO.new
203
- systemu cmd, 'stdin' => code, 'stdout' => stdout
204
-
205
- # Get result
206
- stdout.rewind
207
- stdout.read
208
- end
209
-
210
- SIMON_HIGHLIGHT_OPT_MAP = {
211
- :wrap => '-W',
212
- :include_style => '-I',
213
- :line_numbers => '-l',
214
- }
215
-
216
- # Runs the content through [Highlight](http://www.andre-simon.de/doku/highlight/en/highlight.html).
217
- #
218
- # @api private
219
- #
220
- # @param [String] code The code to colorize
221
- #
222
- # @param [String] language The language the code is written in
223
- #
224
- # @option params [String] :style The style to use
225
- #
226
- # @since 3.2.0
227
- def simon_highlight(code, language, params={})
228
- require 'systemu'
229
-
230
- check_availability('highlight', '--version')
231
-
232
- # Build command
233
- cmd = [ 'highlight', '--syntax', language, '--fragment' ]
234
- params.each do |key, value|
235
- if SIMON_HIGHLIGHT_OPT_MAP[key]
236
- cmd << SIMON_HIGHLIGHT_OPT_MAP[key]
237
- else
238
- # TODO allow passing other options
239
- case key
240
- when :style
241
- cmd << '--style' << params[:style]
242
- end
243
- end
244
- end
245
-
246
- # Run command
247
- stdout = StringIO.new
248
- systemu cmd, 'stdin' => code, 'stdout' => stdout
249
-
250
- # Get result
251
- stdout.rewind
252
- stdout.read
253
- end
254
-
255
- def check_availability(*cmd)
256
- systemu cmd
257
- raise "Could not spawn #{cmd.join(' ')}" if $?.exitstatus != 0
258
- end
259
-
260
- end
261
- end