jekyll-datapage-generator 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/data_page_generator.rb +154 -0
  3. metadata +45 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 41ee3a29126b1c39510e3a317d2afc966f403e54e1c6e1f4c27015b2e760af2c
4
+ data.tar.gz: c8618fd5fe81ec7ca41e3c52c6df4bf42c4dc452f1ec3cc68edc1eb673be66a0
5
+ SHA512:
6
+ metadata.gz: da96126a17faafb1c30e314fa51fd1098ae63c278e6191e5ceb177d48d78ddb781d034da97db501859d188bc896e5608f6c909678a00786387026043becca873
7
+ data.tar.gz: 2bd0e6dcda997034cca0d8271cc6410b6bbafb8ae11ade9b1483527c587269c7a81e50254aa04a354408f6a2e0204c366c48d5d3b618fafff68d1dbe5bb72e50
@@ -0,0 +1,154 @@
1
+ # coding: utf-8
2
+ # Generate pages from individual records in yml files
3
+ # (c) 2014-2016 Adolfo Villafiorita
4
+ # Distributed under the conditions of the MIT License
5
+
6
+ module Jekyll
7
+
8
+ module Sanitizer
9
+ # strip characters and whitespace to create valid filenames, also lowercase
10
+ def sanitize_filename(name)
11
+ if(name.is_a? Integer)
12
+ return name.to_s
13
+ end
14
+ return name.tr(
15
+ "ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÑñÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž",
16
+ "AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz"
17
+ ).downcase.strip.gsub(' ', '-').gsub(/[^\w.-]/, '')
18
+ end
19
+ end
20
+
21
+ # this class is used to tell Jekyll to generate a page
22
+ class DataPage < Page
23
+ include Sanitizer
24
+
25
+ # - site and base are copied from other plugins: to be honest, I am not sure what they do
26
+ #
27
+ # - `index_files` specifies if we want to generate named folders (true) or not (false)
28
+ # - `dir` is the default output directory
29
+ # - `data` is the data defined in `_data.yml` of the record for which we are generating a page
30
+ # - `name` is the key in `data` which determines the output filename
31
+ # - `name_expr` is an expression for generating the output filename
32
+ # - `template` is the name of the template for generating the page
33
+ # - `extension` is the extension for the generated file
34
+ def initialize(site, base, index_files, dir, data, name, name_expr, template, extension)
35
+ @site = site
36
+ @base = base
37
+
38
+ # @dir is the directory where we want to output the page
39
+ # @name is the name of the page to generate
40
+ # @name_expr is an expression for generating the name of the page
41
+ #
42
+ # the value of these variables changes according to whether we
43
+ # want to generate named folders or not
44
+ if name_expr
45
+ record = data
46
+ raw_filename = eval(name_expr)
47
+ if raw_filename == nil
48
+ puts "error (datapage_gen). name_expr '#{name_expr}' generated an empty value in record #{data}"
49
+ return
50
+ end
51
+ else
52
+ raw_filename = data[name]
53
+ if raw_filename == nil
54
+ puts "error (datapage_gen). empty value for field '#{name}' in record #{data}"
55
+ return
56
+ end
57
+ end
58
+
59
+ filename = sanitize_filename(raw_filename).to_s
60
+
61
+ @dir = dir + (index_files ? "/" + filename + "/" : "")
62
+ @name = (index_files ? "index" : filename) + "." + extension.to_s
63
+
64
+ self.process(@name)
65
+ self.read_yaml(File.join(base, '_layouts'), template + ".html")
66
+ self.data['title'] = raw_filename
67
+ # add all the information defined in _data for the current record to the
68
+ # current page (so that we can access it with liquid tags)
69
+
70
+ if data.key?('name')
71
+ data['_name'] = data['name']
72
+ end
73
+
74
+ self.data.merge!(data)
75
+ end
76
+ end
77
+
78
+ class DataPagesGenerator < Generator
79
+ safe true
80
+
81
+ # generate loops over _config.yml/page_gen invoking the DataPage
82
+ # constructor for each record for which we want to generate a page
83
+
84
+ def generate(site)
85
+ # page_gen_dirs determines whether we want to generate index pages
86
+ # (name/index.html) or standard files (name.html). This information
87
+ # is passed to the DataPage constructor, which sets the @dir variable
88
+ # as required by this directive
89
+ index_files = site.config['page_gen-dirs'] == true
90
+
91
+ # data contains the specification of the data for which we want to generate
92
+ # the pages (look at the README file for its specification)
93
+ data = site.config['page_gen']
94
+ if data
95
+ data.each do |data_spec|
96
+ index_files_for_this_data = data_spec['index_files'] != nil ? data_spec['index_files'] : index_files
97
+ template = data_spec['template'] || data_spec['data']
98
+ name = data_spec['name']
99
+ name_expr = data_spec['name_expr']
100
+ dir = data_spec['dir'] || data_spec['data']
101
+ extension = data_spec['extension'] || "html"
102
+
103
+ if site.layouts.key? template
104
+ # records is the list of records defined in _data.yml
105
+ # for which we want to generate different pages
106
+ records = nil
107
+ data_spec['data'].split('.').each do |level|
108
+ if records.nil?
109
+ records = site.data[level]
110
+ else
111
+ records = records[level]
112
+ end
113
+ end
114
+
115
+ # apply filtering conditions:
116
+ # - filter requires the name of a boolean field
117
+ # - filter_condition evals a ruby expression
118
+ records = records.select { |r| r[data_spec['filter']] } if data_spec['filter']
119
+ records = records.select { |record| eval(data_spec['filter_condition']) } if data_spec['filter_condition']
120
+
121
+ records.each do |record|
122
+ site.pages << DataPage.new(site, site.source, index_files_for_this_data, dir, record, name, name_expr, template, extension)
123
+ end
124
+ else
125
+ puts "error (datapage_gen). could not find template #{template}" if not site.layouts.key? template
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ module DataPageLinkGenerator
133
+ include Sanitizer
134
+
135
+ # use it like this: {{input | datapage_url: dir}}
136
+ # to generate a link to a data_page.
137
+ #
138
+ # the filter is smart enough to generate different link styles
139
+ # according to the data_page-dirs directive ...
140
+ #
141
+ # ... however, the filter is not smart enough to support different
142
+ # extensions for filenames.
143
+ #
144
+ # Thus, if you use the `extension` feature of this plugin, you
145
+ # need to generate the links by hand
146
+ def datapage_url(input, dir)
147
+ extension = Jekyll.configuration({})['page_gen-dirs'] ? '/' : '.html'
148
+ "#{dir}/#{sanitize_filename(input)}#{extension}"
149
+ end
150
+ end
151
+
152
+ end
153
+
154
+ Liquid::Template.register_filter(Jekyll::DataPageLinkGenerator)
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-datapage-generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adolfo Villafiorita
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - adolfo.villafiorita@fbk.eu
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/data_page_generator.rb
21
+ homepage:
22
+ licenses:
23
+ - MIT
24
+ metadata:
25
+ source_code_uri: https://github.com/avillafiorita/jekyll-datapage_gen
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.0.3
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Generate one page per yaml record in Jekyll sites.
45
+ test_files: []