nanoc 3.8.0 → 4.0.0a1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (236) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +1 -0
  3. data/Gemfile.lock +322 -0
  4. data/NEWS.md +6 -0
  5. data/bin/nanoc +0 -18
  6. data/lib/nanoc.rb +5 -3
  7. data/lib/nanoc/base.rb +24 -6
  8. data/lib/nanoc/base/checksummer.rb +6 -4
  9. data/lib/nanoc/base/compilation/checksum_store.rb +4 -4
  10. data/lib/nanoc/base/compilation/compiled_content_cache.rb +5 -5
  11. data/lib/nanoc/base/compilation/compiler.rb +60 -65
  12. data/lib/nanoc/base/compilation/compiler_dsl.rb +11 -9
  13. data/lib/nanoc/base/compilation/dependency_tracker.rb +24 -38
  14. data/lib/nanoc/base/compilation/filter.rb +22 -7
  15. data/lib/nanoc/base/compilation/item_rep_proxy.rb +14 -12
  16. data/lib/nanoc/base/compilation/item_rep_recorder_proxy.rb +8 -8
  17. data/lib/nanoc/base/compilation/outdatedness_checker.rb +32 -32
  18. data/lib/nanoc/base/compilation/outdatedness_reasons.rb +3 -1
  19. data/lib/nanoc/base/compilation/rule.rb +9 -7
  20. data/lib/nanoc/base/compilation/rule_context.rb +15 -15
  21. data/lib/nanoc/base/compilation/rule_memory_calculator.rb +3 -3
  22. data/lib/nanoc/base/compilation/rule_memory_store.rb +7 -7
  23. data/lib/nanoc/base/compilation/rules_collection.rb +28 -30
  24. data/lib/nanoc/base/context.rb +2 -2
  25. data/lib/nanoc/base/core_ext/array.rb +17 -25
  26. data/lib/nanoc/base/core_ext/hash.rb +17 -25
  27. data/lib/nanoc/base/core_ext/pathname.rb +4 -2
  28. data/lib/nanoc/base/core_ext/string.rb +5 -3
  29. data/lib/nanoc/base/directed_graph.rb +4 -9
  30. data/lib/nanoc/base/error.rb +7 -0
  31. data/lib/nanoc/base/errors.rb +16 -23
  32. data/lib/nanoc/base/memoization.rb +4 -2
  33. data/lib/nanoc/base/notification_center.rb +3 -1
  34. data/lib/nanoc/base/plugin_registry.rb +14 -21
  35. data/lib/nanoc/base/result_data/item_rep.rb +43 -88
  36. data/lib/nanoc/base/source_data/code_snippet.rb +6 -4
  37. data/lib/nanoc/base/source_data/configuration.rb +3 -1
  38. data/lib/nanoc/base/source_data/data_source.rb +12 -94
  39. data/lib/nanoc/base/source_data/identifier.rb +50 -0
  40. data/lib/nanoc/base/source_data/item.rb +24 -67
  41. data/lib/nanoc/base/source_data/item_array.rb +6 -4
  42. data/lib/nanoc/base/source_data/layout.rb +17 -28
  43. data/lib/nanoc/base/source_data/site.rb +33 -44
  44. data/lib/nanoc/base/store.rb +3 -5
  45. data/lib/nanoc/base/temp_filename_factory.rb +3 -2
  46. data/lib/nanoc/base/views/config.rb +19 -0
  47. data/lib/nanoc/base/views/item.rb +77 -0
  48. data/lib/nanoc/base/views/item_collection.rb +43 -0
  49. data/lib/nanoc/base/views/item_rep.rb +125 -0
  50. data/lib/nanoc/base/views/layout.rb +42 -0
  51. data/lib/nanoc/base/views/layout_collection.rb +26 -0
  52. data/lib/nanoc/base/views/mutable_config.rb +9 -0
  53. data/lib/nanoc/base/views/mutable_item.rb +9 -0
  54. data/lib/nanoc/base/views/mutable_item_collection.rb +18 -0
  55. data/lib/nanoc/base/views/mutable_layout.rb +9 -0
  56. data/lib/nanoc/base/views/mutable_layout_collection.rb +18 -0
  57. data/lib/nanoc/base/views/site.rb +35 -0
  58. data/lib/nanoc/cli.rb +3 -6
  59. data/lib/nanoc/cli/ansi_string_colorizer.rb +2 -0
  60. data/lib/nanoc/cli/cleaning_stream.rb +2 -0
  61. data/lib/nanoc/cli/command_runner.rb +8 -42
  62. data/lib/nanoc/cli/commands/check.rb +2 -2
  63. data/lib/nanoc/cli/commands/compile.rb +19 -34
  64. data/lib/nanoc/cli/commands/create-site.rb +46 -83
  65. data/lib/nanoc/cli/commands/deploy.rb +4 -4
  66. data/lib/nanoc/cli/commands/shell.rb +1 -1
  67. data/lib/nanoc/cli/commands/show-plugins.rb +4 -6
  68. data/lib/nanoc/cli/error_handler.rb +4 -14
  69. data/lib/nanoc/cli/logger.rb +2 -0
  70. data/lib/nanoc/cli/stream_cleaners.rb +1 -0
  71. data/lib/nanoc/cli/stream_cleaners/abstract.rb +2 -0
  72. data/lib/nanoc/cli/stream_cleaners/ansi_colors.rb +2 -0
  73. data/lib/nanoc/cli/stream_cleaners/utf8.rb +2 -0
  74. data/lib/nanoc/data_sources.rb +1 -17
  75. data/lib/nanoc/data_sources/filesystem.rb +6 -37
  76. data/lib/nanoc/data_sources/filesystem_unified.rb +5 -3
  77. data/lib/nanoc/data_sources/filesystem_verbose.rb +5 -3
  78. data/lib/nanoc/data_sources/static.rb +4 -2
  79. data/lib/nanoc/extra.rb +1 -13
  80. data/lib/nanoc/extra/checking.rb +1 -0
  81. data/lib/nanoc/extra/checking/check.rb +4 -2
  82. data/lib/nanoc/extra/checking/checks.rb +1 -0
  83. data/lib/nanoc/extra/checking/checks/css.rb +1 -0
  84. data/lib/nanoc/extra/checking/checks/external_links.rb +2 -0
  85. data/lib/nanoc/extra/checking/checks/html.rb +1 -0
  86. data/lib/nanoc/extra/checking/checks/internal_links.rb +2 -0
  87. data/lib/nanoc/extra/checking/checks/mixed_content.rb +2 -0
  88. data/lib/nanoc/extra/checking/checks/stale.rb +1 -0
  89. data/lib/nanoc/extra/checking/dsl.rb +1 -0
  90. data/lib/nanoc/extra/checking/issue.rb +1 -0
  91. data/lib/nanoc/extra/checking/runner.rb +3 -3
  92. data/lib/nanoc/extra/core_ext/pathname.rb +5 -3
  93. data/lib/nanoc/extra/core_ext/time.rb +4 -2
  94. data/lib/nanoc/extra/deployer.rb +3 -1
  95. data/lib/nanoc/extra/deployers.rb +1 -0
  96. data/lib/nanoc/extra/deployers/fog.rb +2 -0
  97. data/lib/nanoc/extra/deployers/rsync.rb +2 -0
  98. data/lib/nanoc/extra/filesystem_tools.rb +3 -7
  99. data/lib/nanoc/extra/jruby_nokogiri_warner.rb +1 -0
  100. data/lib/nanoc/extra/link_collector.rb +1 -0
  101. data/lib/nanoc/extra/piper.rb +2 -1
  102. data/lib/nanoc/extra/pruner.rb +5 -3
  103. data/lib/nanoc/filters.rb +1 -2
  104. data/lib/nanoc/filters/asciidoc.rb +2 -0
  105. data/lib/nanoc/filters/bluecloth.rb +1 -0
  106. data/lib/nanoc/filters/coffeescript.rb +2 -0
  107. data/lib/nanoc/filters/colorize_syntax.rb +1 -12
  108. data/lib/nanoc/filters/erb.rb +2 -1
  109. data/lib/nanoc/filters/erubis.rb +2 -1
  110. data/lib/nanoc/filters/haml.rb +2 -1
  111. data/lib/nanoc/filters/handlebars.rb +2 -0
  112. data/lib/nanoc/filters/kramdown.rb +1 -0
  113. data/lib/nanoc/filters/less.rb +1 -0
  114. data/lib/nanoc/filters/markaby.rb +1 -0
  115. data/lib/nanoc/filters/maruku.rb +1 -0
  116. data/lib/nanoc/filters/mustache.rb +2 -0
  117. data/lib/nanoc/filters/pandoc.rb +1 -0
  118. data/lib/nanoc/filters/rainpress.rb +1 -0
  119. data/lib/nanoc/filters/rdiscount.rb +1 -0
  120. data/lib/nanoc/filters/rdoc.rb +1 -0
  121. data/lib/nanoc/filters/redcarpet.rb +2 -0
  122. data/lib/nanoc/filters/redcloth.rb +1 -0
  123. data/lib/nanoc/filters/relativize_paths.rb +1 -0
  124. data/lib/nanoc/filters/rubypants.rb +1 -0
  125. data/lib/nanoc/filters/sass.rb +1 -0
  126. data/lib/nanoc/filters/sass/sass_filesystem_importer.rb +2 -0
  127. data/lib/nanoc/filters/slim.rb +3 -1
  128. data/lib/nanoc/filters/typogruby.rb +2 -0
  129. data/lib/nanoc/filters/uglify_js.rb +1 -0
  130. data/lib/nanoc/filters/xsl.rb +2 -0
  131. data/lib/nanoc/filters/yui_compressor.rb +2 -0
  132. data/lib/nanoc/helpers/blogging.rb +30 -30
  133. data/lib/nanoc/helpers/breadcrumbs.rb +2 -2
  134. data/lib/nanoc/helpers/capturing.rb +6 -17
  135. data/lib/nanoc/helpers/filtering.rb +3 -3
  136. data/lib/nanoc/helpers/link_to.rb +3 -3
  137. data/lib/nanoc/helpers/rendering.rb +11 -11
  138. data/lib/nanoc/helpers/xml_sitemap.rb +3 -3
  139. data/lib/nanoc/tasks.rb +1 -0
  140. data/lib/nanoc/tasks/clean.rake +1 -1
  141. data/lib/nanoc/version.rb +1 -1
  142. data/tasks/doc.rake +2 -1
  143. data/tasks/test.rake +7 -1
  144. data/test/base/checksummer_spec.rb +15 -15
  145. data/test/base/core_ext/array_spec.rb +10 -10
  146. data/test/base/core_ext/hash_spec.rb +16 -16
  147. data/test/base/core_ext/pathname_spec.rb +2 -2
  148. data/test/base/core_ext/string_spec.rb +9 -9
  149. data/test/base/temp_filename_factory_spec.rb +3 -7
  150. data/test/base/test_checksum_store.rb +5 -5
  151. data/test/base/test_code_snippet.rb +3 -3
  152. data/test/base/test_compiler.rb +29 -29
  153. data/test/base/test_compiler_dsl.rb +23 -23
  154. data/test/base/test_context.rb +4 -4
  155. data/test/base/test_data_source.rb +17 -7
  156. data/test/base/test_dependency_tracker.rb +29 -29
  157. data/test/base/test_directed_graph.rb +27 -27
  158. data/test/base/test_item.rb +23 -21
  159. data/test/base/test_item_array.rb +18 -18
  160. data/test/base/test_item_rep.rb +76 -76
  161. data/test/base/test_item_rep_recorder_proxy.rb +4 -4
  162. data/test/base/test_layout.rb +8 -16
  163. data/test/base/test_memoization.rb +4 -4
  164. data/test/base/test_notification_center.rb +6 -6
  165. data/test/base/test_outdatedness_checker.rb +18 -18
  166. data/test/base/test_plugin.rb +1 -1
  167. data/test/base/test_rule.rb +2 -2
  168. data/test/base/test_rule_context.rb +5 -5
  169. data/test/base/test_site.rb +62 -38
  170. data/test/base/test_store.rb +2 -2
  171. data/test/cli/commands/test_check.rb +1 -1
  172. data/test/cli/commands/test_compile.rb +24 -18
  173. data/test/cli/commands/test_create_site.rb +5 -7
  174. data/test/cli/commands/test_deploy.rb +1 -1
  175. data/test/data_sources/test_filesystem.rb +22 -67
  176. data/test/data_sources/test_filesystem_unified.rb +10 -23
  177. data/test/data_sources/test_filesystem_verbose.rb +8 -53
  178. data/test/data_sources/test_static.rb +9 -9
  179. data/test/extra/checking/checks/test_stale.rb +1 -1
  180. data/test/extra/core_ext/test_pathname.rb +3 -3
  181. data/test/extra/core_ext/test_time.rb +4 -4
  182. data/test/extra/test_filesystem_tools.rb +1 -1
  183. data/test/filters/test_handlebars.rb +3 -3
  184. data/test/filters/test_less.rb +6 -6
  185. data/test/filters/test_mustache.rb +2 -2
  186. data/test/filters/test_relativize_paths.rb +81 -81
  187. data/test/filters/test_sass.rb +7 -7
  188. data/test/filters/test_xsl.rb +6 -6
  189. data/test/helper.rb +1 -1
  190. data/test/helpers/test_blogging.rb +78 -99
  191. data/test/helpers/test_breadcrumbs.rb +12 -12
  192. data/test/helpers/test_capturing.rb +10 -11
  193. data/test/helpers/test_filtering.rb +6 -6
  194. data/test/helpers/test_rendering.rb +3 -3
  195. data/test/helpers/test_tagging.rb +7 -7
  196. data/test/helpers/test_xml_sitemap.rb +34 -33
  197. metadata +19 -43
  198. data/lib/nanoc/cli/commands/autocompile.rb +0 -69
  199. data/lib/nanoc/cli/commands/create-item.rb +0 -55
  200. data/lib/nanoc/cli/commands/create-layout.rb +0 -68
  201. data/lib/nanoc/cli/commands/sync.rb +0 -32
  202. data/lib/nanoc/cli/commands/update.rb +0 -63
  203. data/lib/nanoc/cli/commands/validate-css.rb +0 -20
  204. data/lib/nanoc/cli/commands/validate-html.rb +0 -20
  205. data/lib/nanoc/cli/commands/validate-links.rb +0 -27
  206. data/lib/nanoc/cli/commands/watch.rb +0 -176
  207. data/lib/nanoc/data_sources/deprecated/delicious.rb +0 -38
  208. data/lib/nanoc/data_sources/deprecated/last_fm.rb +0 -85
  209. data/lib/nanoc/data_sources/deprecated/twitter.rb +0 -34
  210. data/lib/nanoc/extra/auto_compiler.rb +0 -99
  211. data/lib/nanoc/extra/chick.rb +0 -117
  212. data/lib/nanoc/extra/file_proxy.rb +0 -36
  213. data/lib/nanoc/extra/validators.rb +0 -8
  214. data/lib/nanoc/extra/validators/links.rb +0 -18
  215. data/lib/nanoc/extra/validators/w3c.rb +0 -23
  216. data/lib/nanoc/extra/vcs.rb +0 -62
  217. data/lib/nanoc/extra/vcses.rb +0 -15
  218. data/lib/nanoc/extra/vcses/bazaar.rb +0 -21
  219. data/lib/nanoc/extra/vcses/dummy.rb +0 -20
  220. data/lib/nanoc/extra/vcses/git.rb +0 -21
  221. data/lib/nanoc/extra/vcses/mercurial.rb +0 -21
  222. data/lib/nanoc/extra/vcses/subversion.rb +0 -21
  223. data/lib/nanoc/filters/coderay.rb +0 -19
  224. data/lib/nanoc3.rb +0 -3
  225. data/lib/nanoc3/cli.rb +0 -3
  226. data/lib/nanoc3/tasks.rb +0 -3
  227. data/test/cli/commands/test_create_item.rb +0 -10
  228. data/test/cli/commands/test_create_layout.rb +0 -24
  229. data/test/cli/commands/test_sync.rb +0 -27
  230. data/test/cli/commands/test_update.rb +0 -6
  231. data/test/cli/commands/test_watch.rb +0 -74
  232. data/test/extra/test_auto_compiler.rb +0 -437
  233. data/test/extra/test_vcs.rb +0 -18
  234. data/test/extra/validators/test_links.rb +0 -4
  235. data/test/extra/validators/test_w3c.rb +0 -37
  236. data/test/filters/test_coderay.rb +0 -40
@@ -1,38 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::DataSources
4
- # @deprecated Fetch data from online data sources manually instead
5
- class Delicious < Nanoc::DataSource
6
- def items
7
- @items ||= begin
8
- require 'json'
9
-
10
- # Get data
11
- @http_client ||= Nanoc::Extra::CHiCk::Client.new
12
- _status, _headers, data = *@http_client.get("http://feeds.delicious.com/v2/json/#{config[:username]}")
13
-
14
- # Parse as JSON
15
- raw_items = JSON.parse(data)
16
-
17
- # Convert to items
18
- raw_items.enum_with_index.map do |raw_item, i|
19
- # Get data
20
- content = raw_item['n']
21
- attributes = {
22
- url: raw_item['u'],
23
- description: raw_item['d'],
24
- tags: raw_item['t'],
25
- date: Time.parse(raw_item['dt']),
26
- note: raw_item['n'],
27
- author: raw_item['a']
28
- }
29
- identifier = "/#{i}/"
30
- mtime = nil
31
-
32
- # Build item
33
- Nanoc::Item.new(content, attributes, identifier, mtime)
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,85 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::DataSources
4
- # @deprecated Fetch data from online data sources manually instead
5
- class LastFM < Nanoc::DataSource
6
- def items
7
- @items ||= begin
8
- require 'json'
9
- require 'uri'
10
-
11
- # Check configuration
12
- if config[:username].nil?
13
- raise 'LastFM data source requires a username in the configuration'
14
- end
15
- if config[:api_key].nil?
16
- raise 'LastFM data source requires an API key in the configuration'
17
- end
18
-
19
- # Get data
20
- @http_client ||= Nanoc::Extra::CHiCk::Client.new
21
- _status, _headers, data = *@http_client.get(
22
- 'http://ws.audioscrobbler.com/2.0/' \
23
- '?method=user.getRecentTracks' \
24
- '&format=json' \
25
- '&user=' + URI.escape(config[:username]) +
26
- '&api_key=' + URI.escape(config[:api_key])
27
- )
28
-
29
- # Parse as JSON
30
- parsed_data = JSON.parse(data)
31
- raw_items = parsed_data['recenttracks']['track']
32
-
33
- # Convert to items
34
- raw_items.enum_with_index.map do |raw_item, i|
35
- # Get artist data
36
- _artist_status, _artist_headers, artist_data = *@http_client.get(
37
- 'http://ws.audioscrobbler.com/2.0/' \
38
- '?method=artist.getInfo' \
39
- '&format=json' +
40
- (
41
- if raw_item['artist']['mbid'].empty?
42
- '&artist=' + URI.escape(raw_item['artist']['#text'])
43
- else
44
- '&mbid=' + URI.escape(raw_item['artist']['mbid'])
45
- end
46
- ) +
47
- '&api_key=' + URI.escape(config[:api_key])
48
- )
49
-
50
- # Parse as JSON
51
- parsed_artist_data = JSON.parse(artist_data)
52
- raw_artist_info = parsed_artist_data['artist']
53
-
54
- # Build data
55
- content = ''
56
-
57
- # Handle track dates
58
- if raw_item['@attr'] && raw_item['@attr']['nowplaying'] == 'true'
59
- track_played_at = Time.now
60
- now_playing = true
61
- else
62
- track_played_at = Time.parse(raw_item['date']['#text'])
63
- now_playing = false
64
- end
65
-
66
- attributes = {
67
- name: raw_item['name'],
68
- artist: {
69
- name: raw_artist_info['name'],
70
- url: raw_artist_info['url']
71
- },
72
- url: raw_item['url'],
73
- played_at: track_played_at,
74
- now_playing: now_playing
75
- }
76
- identifier = "/#{i}/"
77
- mtime = nil
78
-
79
- # Build item
80
- Nanoc::Item.new(content, attributes, identifier, mtime)
81
- end
82
- end
83
- end
84
- end
85
- end
@@ -1,34 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::DataSources
4
- # @deprecated Fetch data from online data sources manually instead
5
- class Twitter < Nanoc::DataSource
6
- def items
7
- @item ||= begin
8
- require 'json'
9
-
10
- # Get data
11
- @http_client ||= Nanoc::Extra::CHiCk::Client.new
12
- _status, _headers, data = *@http_client.get("http://twitter.com/statuses/user_timeline/#{config[:username]}.json")
13
-
14
- # Parse as JSON
15
- raw_items = JSON.parse(data)
16
-
17
- # Convert to items
18
- raw_items.enum_with_index.map do |raw_item, _i|
19
- # Get data
20
- content = raw_item['text']
21
- attributes = {
22
- created_at: raw_item['created_at'],
23
- source: raw_item['source']
24
- }
25
- identifier = "/#{raw_item['id']}/"
26
- mtime = Time.parse(raw_item['created_at'])
27
-
28
- # Build item
29
- Nanoc::Item.new(content, attributes, identifier, mtime)
30
- end
31
- end
32
- end
33
- end
34
- end
@@ -1,99 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::Extra
4
- # A web server that will automatically compile items as they are requested.
5
- # It also serves static files such as stylesheets and images.
6
- class AutoCompiler
7
- # @return [Nanoc::Site] The site this autocompiler belongs to
8
- attr_reader :site
9
-
10
- # Creates a new autocompiler for the given site.
11
- #
12
- # @param [String] site_path The path to the site to autocompile
13
- def initialize(site_path)
14
- require 'rack'
15
- require 'mime/types'
16
-
17
- # Set site
18
- @site_path = site_path
19
-
20
- # Create mutex to prevent parallel requests
21
- require 'thread'
22
- @mutex = Mutex.new
23
- end
24
-
25
- # Calls the autocompiler. The behaviour of this method is defined by the
26
- # [Rack specification](http://rubydoc.info/github/rack/rack/master/file/SPEC).
27
- #
28
- # @param [Hash] env The environment, as defined by the Rack specification
29
- #
30
- # @return [Array] An array containing the status, the headers, and the
31
- # body, as defined by the Rack specification
32
- def call(env)
33
- @mutex.synchronize do
34
- # Start with a new site
35
- build_site
36
-
37
- # Find rep
38
- path = Rack::Utils.unescape(env['PATH_INFO'])
39
- reps = site.items.map(&:reps).flatten
40
- rep = reps.find do |r|
41
- r.path == path ||
42
- r.raw_path == site.config[:output_dir] + path
43
- end
44
-
45
- # Recompile
46
- site.compile if rep
47
-
48
- # Get paths by appending index filenames
49
- if path =~ /\/$/
50
- possible_paths = site.config[:index_filenames].map { |f| path + f }
51
- else
52
- possible_paths = [path]
53
- end
54
-
55
- # Find matching file
56
- modified_path = possible_paths.find { |f| File.file?(site.config[:output_dir] + f) }
57
- modified_path ||= path
58
-
59
- # Serve using Rack::File
60
- puts "*** serving file #{modified_path}"
61
- res = file_server.call(env.merge('PATH_INFO' => modified_path))
62
- puts "*** done serving file #{modified_path}"
63
- res
64
- end
65
- rescue StandardError, ScriptError => e
66
- # Add compilation stack to env
67
- env['nanoc.stack'] = []
68
- stack.reverse_each do |obj|
69
- if obj.is_a?(Nanoc::ItemRep) # item rep
70
- env['nanoc.stack'] << "[item] #{obj.item.identifier} (rep #{obj.name})"
71
- else # layout
72
- env['nanoc.stack'] << "[layout] #{obj.identifier}"
73
- end
74
- end
75
-
76
- # Re-raise error
77
- raise e
78
- end
79
-
80
- private
81
-
82
- def build_site
83
- @site = Nanoc::Site.new(@site_path)
84
- end
85
-
86
- def mime_type_of(path, fallback)
87
- mime_type = MIME::Types.of(path).first
88
- mime_type.nil? ? fallback : mime_type.simplified
89
- end
90
-
91
- def file_server
92
- @file_server ||= ::Rack::File.new(site.config[:output_dir])
93
- end
94
-
95
- def stack
96
- site.compiler.stack
97
- end
98
- end
99
- end
@@ -1,117 +0,0 @@
1
- # encoding: utf-8
2
-
3
- require 'net/http'
4
- require 'rack'
5
- require 'rack/cache'
6
-
7
- module Nanoc::Extra
8
- # @deprecated Use a HTTP library such as
9
- # [Net::HTTP](http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/) or
10
- # [Curb](https://github.com/taf2/curb) instead.
11
- module CHiCk
12
- # @deprecated Use a HTTP library such as
13
- # [Net::HTTP](http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/) or
14
- # [Curb](https://github.com/taf2/curb) instead.
15
- class Client
16
- DEFAULT_OPTIONS = {
17
- cache: {
18
- metastore: 'file:tmp/rack/cache.meta',
19
- entitystore: 'file:tmp/rack/cache.body'
20
- },
21
- cache_controller: {
22
- max_age: 60
23
- }
24
- }
25
-
26
- def initialize(options = {})
27
- # Get options
28
- @options = DEFAULT_OPTIONS.merge(options)
29
- @options[:cache] = DEFAULT_OPTIONS[:cache].merge(@options[:cache])
30
- @options[:cache_controller] = DEFAULT_OPTIONS[:cache_controller].merge(@options[:cache_controller])
31
- end
32
-
33
- def get(url)
34
- # Build app
35
- options = @options
36
- @app ||= Rack::Builder.new do
37
- use Rack::Cache, options[:cache].merge(verbose: true)
38
- use Nanoc::Extra::CHiCk::CacheController, options[:cache_controller]
39
- run Nanoc::Extra::CHiCk::RackClient
40
- end
41
-
42
- # Build environment for request
43
- env = Rack::MockRequest.env_for(url, method: 'GET')
44
-
45
- # Fetch
46
- puts "[CHiCk] Fetching #{url}..." if $DEBUG
47
- status, headers, body_parts = @app.call(env)
48
- puts "[CHiCk] #{url}: #{headers['X-Rack-Cache']}" if $DEBUG
49
-
50
- # Join body
51
- body = ''
52
- body_parts.each { |part| body << part }
53
-
54
- # Done
55
- [status, headers, body]
56
- end
57
- end
58
-
59
- # @deprecated Use a HTTP library such as
60
- # [Net::HTTP](http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/) or
61
- # [Curb](https://github.com/taf2/curb) instead.
62
- class CacheController
63
- def initialize(app, options = {})
64
- @app = app
65
- @options = options
66
- end
67
-
68
- def call(env)
69
- res = @app.call(env)
70
- unless res[1].key?('Cache-Control') || res[1].key?('Expires')
71
- res[1]['Cache-Control'] = "max-age=#{@options[:max_age]}"
72
- end
73
- res
74
- end
75
- end
76
-
77
- # @deprecated Use a HTTP library such as
78
- # [Net::HTTP](http://ruby-doc.org/stdlib/libdoc/net/http/rdoc/) or
79
- # [Curb](https://github.com/taf2/curb) instead.
80
- class RackClient
81
- METHOD_TO_CLASS_MAPPING = {
82
- 'DELETE' => Net::HTTP::Delete,
83
- 'GET' => Net::HTTP::Get,
84
- 'HEAD' => Net::HTTP::Head,
85
- 'POST' => Net::HTTP::Post,
86
- 'PUT' => Net::HTTP::Put
87
- }
88
-
89
- def self.call(env)
90
- # Build request
91
- request = Rack::Request.new(env)
92
-
93
- # Build headers and strip HTTP_
94
- request_headers = env.reduce({}) do |m, (k, v)|
95
- k =~ /^HTTP_(.*)$/ && v ? m.merge($1.gsub(/_/, '-') => v) : m
96
- end
97
-
98
- # Build Net::HTTP request
99
- http = Net::HTTP.new(request.host, request.port)
100
- net_http_request_class = METHOD_TO_CLASS_MAPPING[request.request_method]
101
- raise ArgumentError, "Unsupported method: #{request.request_method}" if net_http_request_class.nil?
102
- net_http_request = net_http_request_class.new(request.fullpath, request_headers)
103
- net_http_request.body = env['rack.input'].read if %w( POST PUT ).include?(request.request_method)
104
-
105
- # Perform request
106
- http.request(net_http_request) do |response|
107
- # Build Rack response triplet
108
- return [
109
- response.code.to_i,
110
- response.to_hash.reduce({}) { |m, (k, v)| m.merge(k => v[0]) },
111
- [response.body]
112
- ]
113
- end
114
- end
115
- end
116
- end
117
- end
@@ -1,36 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::Extra
4
- # @deprecated Create a File instance directly and use that instead.
5
- class FileProxy
6
- instance_methods.each { |m| undef_method m unless m =~ /^__/ || m.to_s == 'object_id' }
7
-
8
- @@deprecation_warning_shown = false
9
-
10
- def initialize(path)
11
- @path = path
12
- end
13
-
14
- def freeze
15
- end
16
-
17
- def respond_to?(meth, _include_all = false)
18
- file_instance_methods.include?(meth.to_sym)
19
- end
20
-
21
- def method_missing(sym, *args, &block)
22
- unless @@deprecation_warning_shown
23
- $stderr.puts 'WARNING: The :file attribute is deprecated and will be removed in a future version of nanoc. Instead of using this :file attribute, consider manually creating a File object when it’s needed, using the :content_filename, :meta_filename or :filename attributes.'
24
- @@deprecation_warning_shown = true
25
- end
26
-
27
- File.open(@path, 'r') { |io| io.__send__(sym, *args, &block) }
28
- end
29
-
30
- private
31
-
32
- def file_instance_methods
33
- @@file_instance_methods ||= Set.new(File.instance_methods.map(&:to_sym))
34
- end
35
- end
36
- end
@@ -1,8 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::Extra
4
- module Validators
5
- autoload 'W3C', 'nanoc/extra/validators/w3c'
6
- autoload 'Links', 'nanoc/extra/validators/links'
7
- end
8
- end
@@ -1,18 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::Extra::Validators
4
- # @deprecated Use the Checking API or the `check` command instead
5
- class Links
6
- def initialize(_dir, _index_filenames, params = {})
7
- @include_internal = params.key?(:internal) && params[:internal]
8
- @include_external = params.key?(:external) && params[:external]
9
- end
10
-
11
- def run
12
- checks = []
13
- checks << 'ilinks' if options[:internal]
14
- checks << 'elinks' if options[:external]
15
- Nanoc::CLI.run ['check', checks].flatten
16
- end
17
- end
18
- end
@@ -1,23 +0,0 @@
1
- # encoding: utf-8
2
-
3
- module Nanoc::Extra::Validators
4
- # @deprecated Use the Checking API or the `check` command instead
5
- class W3C
6
- def initialize(dir, types)
7
- @dir = dir
8
- @types = types
9
- end
10
-
11
- def run
12
- args = []
13
- types = @types.dup
14
- args << 'html' if types.delete(:html)
15
- args << 'css' if types.delete(:css)
16
- unless types.empty?
17
- raise Nanoc::Errors::GenericTrivial, "unknown type(s) specified: #{types.join(', ')}"
18
- end
19
-
20
- Nanoc::CLI.run(['check', args].flatten)
21
- end
22
- end
23
- end