jekyll-antex 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8af04a4d3122b7e58926aed907120b7bef5e4d5cfb660be03c625f1e0ec48d4
4
- data.tar.gz: 4066bd201baba8f6bdcf724f38ea8991eb011da58f1da13831d32547781ade9c
3
+ metadata.gz: 04f40e277c4a44f0177d6528c0b23cf3fe814d02b0d62bf8b33285649f6ff77a
4
+ data.tar.gz: 86af7068b8c90b17ddeed5e17ecb5406868374c8ab2516dd40805c1ef415fd73
5
5
  SHA512:
6
- metadata.gz: 86ebce737fc0c681b040f329d46aee48198d528a25e0bab5bbbecebc5da5215fb5f462bd385a5f211e97063b45639feaf76cfa0343ac19ec7fd28ccf0091c530
7
- data.tar.gz: 381bc28893d16e442f07567eddee15b08bab1eed15387eb4b3793dbc8e35949a61dcaa0d79e98b1716db5268762d99c6e91304c806a81e914eeaaf111658eee3
6
+ metadata.gz: 8bc58c1233337a4a117b66a6adf30398e166f5b2f87aef95dcac232031c8fd1911e3997a656f3b02d6ee587e5fb76fbe6095ecaba8aafa6527a2ba654c20f348
7
+ data.tar.gz: ad91f452823da9f27eb777238ae95cff9fea0803fe5a8dc0dc142d642aa818dd5192dd832a054526040cf6bbe1f79a49ee83cbdd72607ee99945a05ab7e5f6b7
@@ -7,7 +7,6 @@ module Jekyll
7
7
  class InvalidRegexp < Error; end
8
8
 
9
9
  Alias = Struct.new(:priority, :regexp, :options, keyword_init: true) do
10
-
11
10
  def initialize(*args)
12
11
  super
13
12
  validate_regexp!
@@ -9,6 +9,10 @@ require 'jekyll/antex/options'
9
9
 
10
10
  module Jekyll
11
11
  module Antex
12
+ class << self
13
+ attr_accessor :jobs
14
+ end
15
+
12
16
  class Block < Liquid::Block
13
17
  def initialize(tag, markup, tokens)
14
18
  @markup = markup
@@ -18,45 +22,47 @@ module Jekyll
18
22
  def render(context)
19
23
  options = build_options registers: context.registers, markup: @markup
20
24
  job = ::Antex::Job.new snippet: super, options: options
21
- job.run!
25
+ Jekyll::Antex.jobs.store(job.hash, job)
22
26
  add_static_file context.registers[:site], job
23
- render_html job
27
+ self.class.render_html(job)
24
28
  end
25
29
 
26
30
  private
27
31
 
28
- def render_html(job)
29
- img_tag = render_img_tag src: img_url(job),
30
- set_box: job.set_box
32
+ def self.render_html(job)
33
+ img_tag = render_img_tag job
31
34
  classes = job.options['classes'].join(' ')
32
35
  "<span class='#{classes}'>#{img_tag}</span>"
33
36
  end
34
37
 
35
- def render_img_tag(src:, set_box:, precision: 3)
38
+ def self.render_img_tag(job)
39
+ "<img data-antex='#{job.hash}' src='#{img_url(job)}' />"
40
+ end
41
+
42
+ def self.render_style_attribute(job_set_box, precision: 3)
36
43
  <<~IMG_TAG.gsub(/(\s\s+)/m, ' ').strip!
37
- <img style='margin: #{set_box.mt.round(precision)}ex
38
- #{set_box.mr.round(precision)}ex
39
- #{set_box.mb.round(precision)}ex
40
- #{set_box.ml.round(precision)}ex;
41
- height: #{set_box.th.round(precision)}ex;
42
- width: #{set_box.wd.round(precision)}ex;'
43
- src='#{src}' />
44
+ style='margin: #{job_set_box.mt.round(precision)}ex
45
+ #{job_set_box.mr.round(precision)}ex
46
+ #{job_set_box.mb.round(precision)}ex
47
+ #{job_set_box.ml.round(precision)}ex;
48
+ height: #{job_set_box.th.round(precision)}ex;
49
+ width: #{job_set_box.wd.round(precision)}ex;'
44
50
  IMG_TAG
45
51
  end
46
52
 
47
53
  def add_static_file(site, job)
48
54
  site.static_files << Jekyll::StaticFile.new(
49
- site, *static_file_paths(job)
55
+ site, *self.class.static_file_paths(job)
50
56
  )
51
57
  end
52
58
 
53
- def img_url(job)
59
+ def self.img_url(job)
54
60
  _, dest_dir, filename = static_file_paths job
55
61
  url_path_prefix = job.options['url_path_prefix'] || '/'
56
62
  File.join url_path_prefix, dest_dir, filename
57
63
  end
58
64
 
59
- def static_file_paths(job)
65
+ def self.static_file_paths(job)
60
66
  work_dir_prefix = %r{^#{Regexp.escape job.dirs['work']}(?=\/|$)}
61
67
  # base, dir, name
62
68
  [
@@ -75,5 +81,3 @@ module Jekyll
75
81
  end
76
82
  end
77
83
  end
78
-
79
- Liquid::Template.register_tag('antex', Jekyll::Antex::Block)
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'jekyll/antex/alias'
4
+ require 'jekyll/antex/guard'
5
+ require 'jekyll/antex/stasher'
6
+ require 'jekyll/antex/options'
7
+
8
+ module Jekyll
9
+ module Antex
10
+ module Dealiaser
11
+ def self.run(resource)
12
+ options = build_options(resource)
13
+ guarder = build_guarder(options)
14
+ aliaser = build_aliaser(options)
15
+ content = aliaser.lift(guarder.lift(resource.content))
16
+ aliaser.bake(&method(:dealias_tag))
17
+ guarder.drop(aliaser.drop(content))
18
+ end
19
+
20
+ def self.build_options(resource)
21
+ Jekyll::Antex::Options.build(
22
+ Jekyll::Antex::Options::DEFAULTS,
23
+ resource.site.config['antex'] || {},
24
+ resource.data['antex'] || {}
25
+ )
26
+ end
27
+
28
+ def self.build_guarder(options)
29
+ Jekyll::Antex::Stasher.new(
30
+ options['guards'].values.map(&Guard.method(:new))
31
+ )
32
+ end
33
+
34
+ def self.build_aliaser(options)
35
+ Jekyll::Antex::Stasher.new(
36
+ options['aliases'].values.map(&Alias.method(:new))
37
+ )
38
+ end
39
+
40
+ def self.dealias_tag(match, matcher)
41
+ markup = YAML.safe_load match[:markup] if match.names.include?('markup')
42
+ markup = YAML.dump Options.build(matcher.options || {}, markup || {})
43
+ code = match['code']
44
+ "{% antex #{markup} %}#{code}{% endantex %}"
45
+ end
46
+ end
47
+ end
48
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Antex
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.0'
6
6
  end
7
7
  end
data/lib/jekyll/antex.rb CHANGED
@@ -3,10 +3,51 @@
3
3
  require 'antex'
4
4
 
5
5
  require 'jekyll/antex/version'
6
- require 'jekyll/antex/error'
7
- require 'jekyll/antex/options'
8
- require 'jekyll/antex/guard'
9
- require 'jekyll/antex/stasher'
10
- require 'jekyll/antex/alias'
11
- require 'jekyll/antex/generator'
6
+ require 'jekyll/antex/dealiaser'
12
7
  require 'jekyll/antex/block'
8
+
9
+ require 'byebug'
10
+
11
+ module Jekyll
12
+ module Antex
13
+ def self.run_jobs
14
+ jobs_count = Jekyll::Antex.jobs.count
15
+ Jekyll.logger.writer << "Compiling TeX: ".rjust(20)
16
+ Jekyll::Antex.jobs.values.each_with_index do |job, i|
17
+ progress = "job " + i.next.to_s.rjust(jobs_count.to_s.length) + " of #{jobs_count} "
18
+ Jekyll.logger.writer << progress
19
+ job.run!
20
+ Jekyll.logger.writer << "\b" * progress.length
21
+ end
22
+ Jekyll.logger.writer << "#{jobs_count} jobs completed.\n"
23
+ end
24
+
25
+ def self.inject_style_attributes(output)
26
+ output.gsub(/data-antex="(?<hash>.*?)"/) do
27
+ job = Jekyll::Antex.jobs[Regexp.last_match[:hash]]
28
+ Jekyll::Antex::Block.render_style_attribute(job.set_box)
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ # NOTE: I'm conflicted, but regexp flags in yaml must be easy to use.
35
+ SafeYAML::OPTIONS[:whitelisted_tags].push('!ruby/regexp')
36
+
37
+ Jekyll::Hooks.register :site, :pre_render do
38
+ Jekyll::Antex.jobs = {}
39
+ end
40
+
41
+ # NOTE: we're not registering :posts since they are included in :documents.
42
+ Jekyll::Hooks.register [:documents, :pages], :pre_render do |resource|
43
+ resource.content = Jekyll::Antex::Dealiaser.run(resource)
44
+ end
45
+
46
+ Liquid::Template.register_tag('antex', Jekyll::Antex::Block)
47
+
48
+ Jekyll::Hooks.register :site, :post_render do |site|
49
+ Jekyll::Antex.run_jobs
50
+ [*site.pages, *site.documents].each do |resource|
51
+ resource.output = Jekyll::Antex.inject_style_attributes(resource.output)
52
+ end
53
+ end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-antex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paolo Brasolin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-11 00:00:00.000000000 Z
11
+ date: 2018-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: jekyll
14
+ name: antex
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '3.8'
19
+ version: '0.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '3.8'
26
+ version: '0.1'
27
27
  - !ruby/object:Gem::Dependency
28
- name: antex
28
+ name: jekyll
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.1'
33
+ version: '3.8'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.1'
40
+ version: '3.8'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: byebug
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -162,9 +162,9 @@ files:
162
162
  - lib/jekyll/antex.rb
163
163
  - lib/jekyll/antex/alias.rb
164
164
  - lib/jekyll/antex/block.rb
165
+ - lib/jekyll/antex/dealiaser.rb
165
166
  - lib/jekyll/antex/defaults.yml
166
167
  - lib/jekyll/antex/error.rb
167
- - lib/jekyll/antex/generator.rb
168
168
  - lib/jekyll/antex/guard.rb
169
169
  - lib/jekyll/antex/options.rb
170
170
  - lib/jekyll/antex/stasher.rb
@@ -1,67 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'jekyll'
4
- require 'jekyll/utils'
5
-
6
- require 'jekyll/antex/alias'
7
- require 'jekyll/antex/guard'
8
- require 'jekyll/antex/stasher'
9
- require 'jekyll/antex/options'
10
-
11
- # NOTE: I'm pretty conflicted about this.
12
- SafeYAML::OPTIONS[:whitelisted_tags].push('!ruby/regexp')
13
-
14
- module Jekyll
15
- module Antex
16
- class Generator < Jekyll::Generator
17
- def generate(site)
18
- site.pages.each do |page|
19
- dealias_resource_content! site: site, resource: page
20
- end
21
-
22
- # NOTE: jekyll deprecated direct access to site.posts.each
23
- site.posts.docs.each do |post|
24
- dealias_resource_content! site: site, resource: post
25
- end
26
- end
27
-
28
- private
29
-
30
- def dealias_resource_content!(site:, resource:)
31
- options = build_options site: site, resource: resource
32
- guardian = Jekyll::Antex::Stasher.new(build_guards(options['guards']))
33
- dealiaser = Jekyll::Antex::Stasher.new(build_aliases(options['aliases']))
34
- resource.content = guardian.lift(resource.content)
35
- resource.content = dealiaser.lift(resource.content)
36
- dealiaser.bake(&self.class.method(:dealias_tag))
37
- resource.content = dealiaser.drop(resource.content)
38
- resource.content = guardian.drop(resource.content)
39
- end
40
-
41
- def build_options(site:, resource:)
42
- Jekyll::Antex::Options.build Jekyll::Antex::Options::DEFAULTS,
43
- site.config['antex'] || {},
44
- resource.data['antex'] || {}
45
- end
46
-
47
- def build_guards(options_hash)
48
- options_hash.values.map do |args|
49
- Guard.new Jekyll::Utils.symbolize_hash_keys(args)
50
- end
51
- end
52
-
53
- def build_aliases(options_hash)
54
- options_hash.values.map do |args|
55
- Alias.new Jekyll::Utils.symbolize_hash_keys(args)
56
- end
57
- end
58
-
59
- def self.dealias_tag(match, matcher)
60
- markup = YAML.safe_load match[:markup] if match.names.include?('markup')
61
- markup = YAML.dump Options.build(matcher.options || {}, markup || {})
62
- code = match['code']
63
- "{% antex #{markup} %}#{code}{% endantex %}"
64
- end
65
- end
66
- end
67
- end