polites-nanoc 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2e36f9557f220439f562271b7490f2864818ca7aa6942f6150910d13ecc8c3b4
4
+ data.tar.gz: 740db988f7f1cd5fc3970b39e60a2e74d7931f11c214665390bc00c685c7a5e6
5
+ SHA512:
6
+ metadata.gz: c4233ba9d5089ed058daa1c9568d3bbc155a0aa67b6a5d0a3d81f266d6303368f4cfbf67d47caf57de3259816c6fffe2031d835dfce4eeeec74bb5120761e860
7
+ data.tar.gz: 72170642a9f068bf90ec1381efb95af4b68d0c8c948609dfd1339367896906908a6d4e7806e0a663ab958baa381c4af35de356b02ab8a58a09cf5093ee820075
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './nanoc/data_source'
4
+ require_relative './nanoc/extract_file_filter'
5
+ require_relative './nanoc/embedded_images_filter'
6
+
7
+ module Polites
8
+ # The {Polites::Nanoc} module provides integration with the
9
+ # [Nanoc](https://nanoc.ws) static site generator. It allows you to
10
+ # configure a Polites external directory as a data source in a Nanoc site,
11
+ # so you can transform Polites files straight into HTML documents.
12
+ #
13
+ # This gem consists of the following parts:
14
+ #
15
+ # * {Polites::Nanoc::DataSource} implements the data source that reads .ulyz
16
+ # files from disk and creates Nanoc HTML items from them.
17
+ # * {Polites::Nanoc::EmbeddedImagesFilter} implements a filter you can use to
18
+ # transform the image paths in your HTML to the pats generated by your Nanoc
19
+ # rules.
20
+ # * {Polites::Nanoc::ExtractFileFilter} is a filter that will extract embedded
21
+ # files from the .ulyz zip file to actual output files on disk in your Nanoc
22
+ # site.
23
+ #
24
+ # @example Nanoc configuration using Polites data source
25
+ # # in nanoc.yaml
26
+ # data_sources:
27
+ # - type: polites
28
+ # items_root: /articles/
29
+ # path: path/to/articles
30
+ # @example Require Ulussyes::Nanoc in your site
31
+ # # lib/default.rb
32
+ # require 'polites/nanoc'
33
+ # @example Nanoc rule to compile Ulussyes articles
34
+ # compile "/articles/*.ulyz" do
35
+ # filter :polites_embedded_images
36
+ # layout "/default.*"
37
+ # write item.identifier.without_ext + "/index.html"
38
+ # end
39
+ # @example Nanoc rule to extract embedded media
40
+ # compile(%r{\A/articles/.+\.ulyz/media/.+\Z}) do
41
+ # filter :extract_file
42
+ # write item.identifier.to_s.sub(".ulyz", "")
43
+ # end
44
+ module Nanoc
45
+ end
46
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pathname'
4
+ require_relative '../settings'
5
+ require_relative '../parser'
6
+ require_relative '../file'
7
+ require_relative '../html_formatter'
8
+
9
+ module Polites
10
+ module Nanoc
11
+ # A data source for Nanoc that creates Nanoc content items from Polites files
12
+ # in a particular directory.
13
+ class DataSource < ::Nanoc::DataSource
14
+ identifier :polites
15
+
16
+ def up
17
+ @root = Pathname(@config[:path])
18
+ @settings = Settings.from_directory(@root)
19
+ @extension = ".#{@settings['defaultPathExtensions']}"
20
+ @input_files = @root.glob("*#{@extension}")
21
+ @parser = Polites::Parser.new
22
+ @formatter = Polites::HtmlFormatter.new
23
+ end
24
+
25
+ def items
26
+ @input_files.flat_map do |input_file|
27
+ File.open(input_file) do |file|
28
+ sheet = @parser.parse_sheet(file.content)
29
+
30
+ inline_file_items = sheet.inline_files.map do |image|
31
+ build_file_item(file.media(image.image), image.image, input_file, image.filename)
32
+ end
33
+
34
+ file_items = sheet.attached_files.map do |id|
35
+ build_file_item(file.media(id), id, input_file)
36
+ end
37
+
38
+ [
39
+ new_item(
40
+ @formatter.call(sheet),
41
+ {
42
+ keywords: sheet.keywords,
43
+ image: sheet.attached_files.first,
44
+ image_caption: sheet.notes.any? ? @formatter.call(sheet.notes.first) : nil,
45
+ inline_file_items: inline_file_items,
46
+ filename: input_file.to_s,
47
+ mtime: input_file.mtime
48
+ },
49
+ identifier(input_file)
50
+ ),
51
+ *inline_file_items,
52
+ *file_items
53
+ ]
54
+ end
55
+ end
56
+ end
57
+
58
+ private
59
+
60
+ def build_file_item(entry, id, input_file, filename = nil)
61
+ p = filename ? Pathname(filename) : Pathname(entry.name).basename
62
+ i = "#{identifier(input_file)}/media#{identifier(p, p.extname)}"
63
+ new_item(
64
+ input_file.expand_path.to_s,
65
+ {
66
+ explicit_filename: filename,
67
+ id: id,
68
+ subpath: entry.name,
69
+ mtime: input_file.mtime
70
+ },
71
+ i,
72
+ binary: true
73
+ )
74
+ end
75
+
76
+ def identifier(path, extension = @extension)
77
+ "/#{path
78
+ .relative_path_from(@root)
79
+ .basename(extension)
80
+ .to_s
81
+ .then { |s| underscore(s) }}#{extension}"
82
+ end
83
+
84
+ def underscore(str)
85
+ str
86
+ .gsub(/[^a-zA-Z0-9\-_]/, '-')
87
+ .squeeze('-')
88
+ .gsub(/^-*|-*$/, '')
89
+ .downcase
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polites
4
+ module Nanoc
5
+ # Nanoc filter for replacing the Polites-generated filename to images with
6
+ # actual output filenames as generated by Nanoc.
7
+ class EmbeddedImagesFilter < ::Nanoc::Filter
8
+ identifier :polites_embedded_images
9
+
10
+ def run(content, _params = {})
11
+ return content unless @item[:inline_file_items]&.any?
12
+
13
+ @item[:inline_file_items].inject(content) do |acc, inline_file_item|
14
+ actual_item = @items.find do |item|
15
+ item.attributes[:id] == inline_file_item.attributes[:id]
16
+ end
17
+ acc.gsub(/(?<=src=")(#{actual_item.attributes[:explicit_filename]}|#{actual_item.attributes[:id]})(?=")/, actual_item.path)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../file'
4
+
5
+ module Polites
6
+ module Nanoc
7
+ # Nanoc binary filter to extract files from a zip file to a given output file.
8
+ # This allows a single Polites file to be linked to multiple Nanoc items,
9
+ # which are extracted when needed during the compilation process.
10
+ class ExtractFileFilter < ::Nanoc::Filter
11
+ identifier :extract_file
12
+ type :binary
13
+
14
+ def run(filename, _params = {})
15
+ File.open(filename) do |f|
16
+ f.extract_to(@item[:subpath], output_filename)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/polites/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'polites-nanoc'
7
+ spec.version = Polites::VERSION
8
+ spec.authors = ['Arjan van der Gaag']
9
+ spec.email = ['arjan@arjanvandergaag.nl']
10
+
11
+ spec.summary = 'Integrate the Ulysess gem with Nanoc.'
12
+ spec.description = 'Use Polites\' .ulyz files as a data source in static sites generated with Nanoc.'
13
+ spec.homepage = 'https://github.com/avdgaag/polites'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/avdgaag/polites'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/avdgaag/polites'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }.grep(/nanoc/)
27
+ end
28
+ spec.require_paths = ['lib']
29
+ spec.add_dependency 'nanoc', '~> 4'
30
+ spec.add_dependency 'polites', "~> #{Polites::VERSION}"
31
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polites-nanoc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Arjan van der Gaag
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-11-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nanoc
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: polites
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.1.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.1.0
41
+ description: Use Polites' .ulyz files as a data source in static sites generated with
42
+ Nanoc.
43
+ email:
44
+ - arjan@arjanvandergaag.nl
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - lib/polites/nanoc.rb
50
+ - lib/polites/nanoc/data_source.rb
51
+ - lib/polites/nanoc/embedded_images_filter.rb
52
+ - lib/polites/nanoc/extract_file_filter.rb
53
+ - polites-nanoc.gemspec
54
+ homepage: https://github.com/avdgaag/polites
55
+ licenses:
56
+ - MIT
57
+ metadata:
58
+ allowed_push_host: https://rubygems.org
59
+ homepage_uri: https://github.com/avdgaag/polites
60
+ source_code_uri: https://github.com/avdgaag/polites
61
+ changelog_uri: https://github.com/avdgaag/polites
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: 2.4.0
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.1.4
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Integrate the Ulysess gem with Nanoc.
81
+ test_files: []