klenod-build 0.0.32 → 0.0.34

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dea7b4a7c9bf636c890c494dab1929c66402d4be72e092e3b77ce314e2778d12
4
- data.tar.gz: d8376d7d25e34bc2dada8a59ecb778cd577afa3c5819376011da3bbe79bd7c07
3
+ metadata.gz: 5c9113a2ead2ad79421171b10bff81a17d78ab553c7ee07ca65a30fe174cc1ce
4
+ data.tar.gz: aac9493e330c9705ffef7ed136edd78ad148c89e9b282fbdde835609230aff92
5
5
  SHA512:
6
- metadata.gz: 81cac761f9eba7fe96e613a4d2bf4d61841dd50e2d5e91cdbfc6e01a8050d51a17ee4d716214cc2520d712c3f3060929eb3c03684fbcbdf34e9c0312d9e19469
7
- data.tar.gz: 5bafa51702e1a64c079b6210088eb24e8423ac5cf6b997fc55e670843b9bd31f862f6c7853a31ea98a4dae1451a689fdb5e8d7b055b449a76ae1819d448df79b
6
+ metadata.gz: '09af24e68044c29489757c960e4040b8b471705b9ce5bf7193982d9dbe1fc79c48624304a2ad4911a96f7715b52ee9eacad13e44246e9a305618037d6c53845f'
7
+ data.tar.gz: 3f05c599da56eb48373e8084c3f8f33047fc443398a29e52218b9addc61e230581943fa6627be3fd0c70885db2293b68a49a762a316b8aa2d2a0ded2ac56b8ad
@@ -16,13 +16,16 @@ module Klenod
16
16
  @extensions = extensions.freeze
17
17
  @path_prefix = path_prefix.to_s
18
18
  @spelling_index = nil
19
+ @directory_entries = {}
19
20
  end
20
21
 
21
- # Forget the cached file listing behind did-you-mean suggestions. The
22
- # owning resolver clears it whenever files change, so a suggestion never
23
- # walks the source tree more than once between file events.
22
+ # Forget the cached directory listings, and the file listing behind
23
+ # did-you-mean suggestions. The owning resolver clears them whenever
24
+ # files change, so neither reads the source tree more than once
25
+ # between file events.
24
26
  def clear_cache
25
27
  @spelling_index = nil
28
+ @directory_entries = {}
26
29
  end
27
30
 
28
31
  def resolve(relative_path)
@@ -101,12 +104,19 @@ module Klenod
101
104
  end
102
105
  end
103
106
 
107
+ # Kept until the cache is cleared. Every import of a file reads the
108
+ # directories above it, so a directory of a thousand modules was
109
+ # listed a thousand times.
104
110
  def directory_entries(directory)
105
- return [] unless directory.directory?
111
+ @directory_entries[directory.to_s] ||= read_directory_entries(directory)
112
+ end
113
+
114
+ def read_directory_entries(directory)
115
+ return Set.new unless directory.directory?
106
116
 
107
- directory.children.map { it.basename.to_s }
117
+ directory.children.to_set { it.basename.to_s }
108
118
  rescue SystemCallError
109
- []
119
+ Set.new
110
120
  end
111
121
 
112
122
  SpellingIndex = Data.define(:aliases, :checker)
@@ -555,17 +555,19 @@ module Klenod
555
555
  def module_ids_for_assets(record_or_module_id, recursive:, type: nil)
556
556
  return Array(record_or_module_id).map { |module_ref| module_id_for(module_ref) }.uniq unless recursive
557
557
 
558
- seen = []
558
+ # A hash, because a page of a large site reaches thousands of modules
559
+ # and each of them asks whether it was seen.
560
+ seen = {}
559
561
  Array(record_or_module_id).flat_map do |module_ref|
560
562
  ordered_module_ids_for_assets(module_id_for(module_ref), seen, type: type)
561
563
  end
562
564
  end
563
565
 
564
566
  def ordered_module_ids_for_assets(module_id, seen, type: nil)
565
- return [] if seen.include?(module_id)
567
+ return [] if seen.key?(module_id)
566
568
  return [] unless @records.key?(module_id)
567
569
 
568
- seen << module_id
570
+ seen[module_id] = true
569
571
  record = @records.fetch(module_id)
570
572
  dependency_ids =
571
573
  record
@@ -6,6 +6,7 @@ require "toml-rb"
6
6
  require "yaml"
7
7
 
8
8
  require_relative "../plugin"
9
+ require_relative "../ruby_literal"
9
10
  require_relative "../source_error"
10
11
  require_relative "../transform_result"
11
12
 
@@ -64,42 +65,9 @@ module Klenod
64
65
 
65
66
  def module_source(data)
66
67
  <<~RUBY
67
- Default = #{ruby_literal(data)}
68
+ Default = #{RubyLiteral.frozen(data)}
68
69
  RUBY
69
70
  end
70
-
71
- # `inspect` is not Ruby source for every parsed value: dates, times,
72
- # and non-finite floats need constructor calls instead.
73
- def ruby_literal(value)
74
- case value
75
- when Hash
76
- entries = value.map { |key, entry| "#{ruby_literal(key)} => #{ruby_literal(entry)}" }
77
- "{#{entries.join(", ")}}"
78
- when Array
79
- "[#{value.map { ruby_literal(it) }.join(", ")}]"
80
- when Float
81
- float_literal(value)
82
- when Time
83
- zone = value.utc? ? "UTC" : value.utc_offset
84
- "::Time.at(#{value.to_i}, #{value.nsec}, :nsec, in: #{zone.inspect})"
85
- when Date
86
- "::Date.new(#{value.year}, #{value.month}, #{value.day})"
87
- when nil, true, false, String, Symbol, Integer
88
- value.inspect
89
- else
90
- raise TypeError, "Cannot write #{value.class} as data module source"
91
- end
92
- end
93
-
94
- def float_literal(value)
95
- if value.nan?
96
- "::Float::NAN"
97
- elsif value.infinite?
98
- value.positive? ? "::Float::INFINITY" : "-::Float::INFINITY"
99
- else
100
- value.inspect
101
- end
102
- end
103
71
  end
104
72
  end
105
73
 
@@ -161,6 +161,15 @@
161
161
  "unitsPerEm": 2048,
162
162
  "xWidthAvg": 649
163
163
  },
164
+ "Agrave Pro": {
165
+ "familyName": "Agrave Pro",
166
+ "category": "handwriting",
167
+ "ascent": 1050,
168
+ "descent": -390,
169
+ "lineGap": 0,
170
+ "unitsPerEm": 1000,
171
+ "xWidthAvg": 423
172
+ },
164
173
  "Agu Display": {
165
174
  "familyName": "Agu Display",
166
175
  "category": "display",
@@ -2832,7 +2841,7 @@
2832
2841
  "descent": -245,
2833
2842
  "lineGap": 330,
2834
2843
  "unitsPerEm": 1000,
2835
- "xWidthAvg": 451
2844
+ "xWidthAvg": 450
2836
2845
  },
2837
2846
  "Castoro Titling": {
2838
2847
  "familyName": "Castoro Titling",
@@ -2841,7 +2850,7 @@
2841
2850
  "descent": -245,
2842
2851
  "lineGap": 330,
2843
2852
  "unitsPerEm": 1000,
2844
- "xWidthAvg": 613
2853
+ "xWidthAvg": 608
2845
2854
  },
2846
2855
  "Catamaran": {
2847
2856
  "familyName": "Catamaran",
@@ -3167,6 +3176,15 @@
3167
3176
  "unitsPerEm": 1000,
3168
3177
  "xWidthAvg": 589
3169
3178
  },
3179
+ "Clarity City": {
3180
+ "familyName": "Clarity City",
3181
+ "category": "sans-serif",
3182
+ "ascent": 994,
3183
+ "descent": -306,
3184
+ "lineGap": 0,
3185
+ "unitsPerEm": 1000,
3186
+ "xWidthAvg": 479
3187
+ },
3170
3188
  "Clicker Script": {
3171
3189
  "familyName": "Clicker Script",
3172
3190
  "category": "handwriting",
@@ -4706,6 +4724,15 @@
4706
4724
  "unitsPerEm": 1000,
4707
4725
  "xWidthAvg": 379
4708
4726
  },
4727
+ "Firjar": {
4728
+ "familyName": "Firjar",
4729
+ "category": "sans-serif",
4730
+ "ascent": 1000,
4731
+ "descent": -400,
4732
+ "lineGap": 0,
4733
+ "unitsPerEm": 1000,
4734
+ "xWidthAvg": 438
4735
+ },
4709
4736
  "Fjalla One": {
4710
4737
  "familyName": "Fjalla One",
4711
4738
  "category": "sans-serif",
@@ -6461,6 +6488,15 @@
6461
6488
  "unitsPerEm": 1000,
6462
6489
  "xWidthAvg": 288
6463
6490
  },
6491
+ "Isometra": {
6492
+ "familyName": "Isometra",
6493
+ "category": "serif",
6494
+ "ascent": 1050,
6495
+ "descent": -250,
6496
+ "lineGap": 0,
6497
+ "unitsPerEm": 1000,
6498
+ "xWidthAvg": 683
6499
+ },
6464
6500
  "Istok Web": {
6465
6501
  "familyName": "Istok Web",
6466
6502
  "category": "sans-serif",
@@ -7307,6 +7343,15 @@
7307
7343
  "unitsPerEm": 1000,
7308
7344
  "xWidthAvg": 412
7309
7345
  },
7346
+ "Kripa": {
7347
+ "familyName": "Kripa",
7348
+ "category": "sans-serif",
7349
+ "ascent": 1080,
7350
+ "descent": -390,
7351
+ "lineGap": 0,
7352
+ "unitsPerEm": 1000,
7353
+ "xWidthAvg": 481
7354
+ },
7310
7355
  "Kristi": {
7311
7356
  "familyName": "Kristi",
7312
7357
  "category": "handwriting",
@@ -7802,6 +7847,15 @@
7802
7847
  "unitsPerEm": 1000,
7803
7848
  "xWidthAvg": 450
7804
7849
  },
7850
+ "Libre Caslon Condensed": {
7851
+ "familyName": "Libre Caslon Condensed",
7852
+ "category": "serif",
7853
+ "ascent": 2061,
7854
+ "descent": -539,
7855
+ "lineGap": 0,
7856
+ "unitsPerEm": 2000,
7857
+ "xWidthAvg": 767
7858
+ },
7805
7859
  "Libre Caslon Display": {
7806
7860
  "familyName": "Libre Caslon Display",
7807
7861
  "category": "serif",
@@ -7895,8 +7949,8 @@
7895
7949
  "Linefont": {
7896
7950
  "familyName": "Linefont",
7897
7951
  "category": "display",
7898
- "ascent": 1200,
7899
- "descent": -200,
7952
+ "ascent": 1300,
7953
+ "descent": -300,
7900
7954
  "lineGap": 0,
7901
7955
  "unitsPerEm": 1000,
7902
7956
  "xWidthAvg": 100
@@ -14039,6 +14093,69 @@
14039
14093
  "unitsPerEm": 1000,
14040
14094
  "xWidthAvg": 464
14041
14095
  },
14096
+ "Redaction": {
14097
+ "familyName": "Redaction",
14098
+ "category": "serif",
14099
+ "ascent": 900,
14100
+ "descent": -300,
14101
+ "lineGap": 0,
14102
+ "unitsPerEm": 1000,
14103
+ "xWidthAvg": 443
14104
+ },
14105
+ "Redaction 10": {
14106
+ "familyName": "Redaction 10",
14107
+ "category": "serif",
14108
+ "ascent": 900,
14109
+ "descent": -300,
14110
+ "lineGap": 0,
14111
+ "unitsPerEm": 1000,
14112
+ "xWidthAvg": 442
14113
+ },
14114
+ "Redaction 100": {
14115
+ "familyName": "Redaction 100",
14116
+ "category": "display",
14117
+ "ascent": 900,
14118
+ "descent": -300,
14119
+ "lineGap": 0,
14120
+ "unitsPerEm": 1000,
14121
+ "xWidthAvg": 436
14122
+ },
14123
+ "Redaction 20": {
14124
+ "familyName": "Redaction 20",
14125
+ "category": "serif",
14126
+ "ascent": 900,
14127
+ "descent": -300,
14128
+ "lineGap": 0,
14129
+ "unitsPerEm": 1000,
14130
+ "xWidthAvg": 441
14131
+ },
14132
+ "Redaction 35": {
14133
+ "familyName": "Redaction 35",
14134
+ "category": "display",
14135
+ "ascent": 900,
14136
+ "descent": -300,
14137
+ "lineGap": 0,
14138
+ "unitsPerEm": 1000,
14139
+ "xWidthAvg": 442
14140
+ },
14141
+ "Redaction 50": {
14142
+ "familyName": "Redaction 50",
14143
+ "category": "display",
14144
+ "ascent": 900,
14145
+ "descent": -300,
14146
+ "lineGap": 0,
14147
+ "unitsPerEm": 1000,
14148
+ "xWidthAvg": 444
14149
+ },
14150
+ "Redaction 70": {
14151
+ "familyName": "Redaction 70",
14152
+ "category": "display",
14153
+ "ascent": 900,
14154
+ "descent": -300,
14155
+ "lineGap": 0,
14156
+ "unitsPerEm": 1000,
14157
+ "xWidthAvg": 443
14158
+ },
14042
14159
  "Reddit Mono": {
14043
14160
  "familyName": "Reddit Mono",
14044
14161
  "category": "monospace",
@@ -16435,13 +16552,22 @@
16435
16552
  },
16436
16553
  "Tiny5": {
16437
16554
  "familyName": "Tiny5",
16438
- "category": "sans-serif",
16555
+ "category": "display",
16439
16556
  "ascent": 896,
16440
16557
  "descent": -256,
16441
16558
  "lineGap": 0,
16442
16559
  "unitsPerEm": 1024,
16443
16560
  "xWidthAvg": 438
16444
16561
  },
16562
+ "Tiny5 Duo": {
16563
+ "familyName": "Tiny5 Duo",
16564
+ "category": "display",
16565
+ "ascent": 896,
16566
+ "descent": -256,
16567
+ "lineGap": 0,
16568
+ "unitsPerEm": 1024,
16569
+ "xWidthAvg": 640
16570
+ },
16445
16571
  "Tiro Bangla": {
16446
16572
  "familyName": "Tiro Bangla",
16447
16573
  "category": "serif",
@@ -17165,8 +17291,8 @@
17165
17291
  "Wavefont": {
17166
17292
  "familyName": "Wavefont",
17167
17293
  "category": "display",
17168
- "ascent": 1200,
17169
- "descent": -200,
17294
+ "ascent": 1300,
17295
+ "descent": -300,
17170
17296
  "lineGap": 0,
17171
17297
  "unitsPerEm": 1000,
17172
17298
  "xWidthAvg": 100
@@ -17288,6 +17414,15 @@
17288
17414
  "unitsPerEm": 1000,
17289
17415
  "xWidthAvg": 500
17290
17416
  },
17417
+ "Xmind": {
17418
+ "familyName": "Xmind",
17419
+ "category": "sans-serif",
17420
+ "ascent": 2053,
17421
+ "descent": -610,
17422
+ "lineGap": 0,
17423
+ "unitsPerEm": 2048,
17424
+ "xWidthAvg": 909
17425
+ },
17291
17426
  "Yaldevi": {
17292
17427
  "familyName": "Yaldevi",
17293
17428
  "category": "sans-serif",
@@ -17403,7 +17538,7 @@
17403
17538
  "descent": -288,
17404
17539
  "lineGap": 0,
17405
17540
  "unitsPerEm": 1000,
17406
- "xWidthAvg": 516
17541
+ "xWidthAvg": 501
17407
17542
  },
17408
17543
  "Young Serif": {
17409
17544
  "familyName": "Young Serif",
@@ -5,7 +5,7 @@ Update them to the latest Capsize revision from the Klenod repository root:
5
5
  bundle exec rake google_fonts:metrics:update
6
6
 
7
7
  Project: https://github.com/seek-oss/capsize
8
- Commit: 8df97f098adc19d8a6d33df301757d7e9670bae6
8
+ Commit: 89b2a35dffe676f2fd06d6acb5e5880c33b15199
9
9
  Source: packages/metrics/src/entireMetricsCollection.json
10
10
 
11
11
  MIT License
@@ -100,7 +100,9 @@ module Klenod
100
100
  end
101
101
 
102
102
  if (old_attributes = attribute_hashes[:old])
103
- literal.concat(literal_class_names_from_old_attributes(old_attributes))
103
+ folded_shorthand, folded_literal = folded_class_names_from_old_attributes(old_attributes)
104
+ shorthand.concat(folded_shorthand)
105
+ literal.concat(folded_literal)
104
106
  end
105
107
 
106
108
  {shorthand: shorthand, literal: literal}
@@ -222,20 +224,26 @@ module Klenod
222
224
  attribute_sequence?(rest)
223
225
  end
224
226
 
225
- def literal_class_names_from_old_attributes(source)
226
- parsed = ::Haml::AttributeParser.parse(source)
227
- return [] unless parsed&.key?("class")
227
+ # Haml folds an old-style hash into the tag's static attributes only
228
+ # when `parse_static_hash` accepts it (hashrocket pairs of literals),
229
+ # and then drops the hash source. Any other hash, such as
230
+ # `{class: "plain"}`, stays a dynamic attribute that the transformer
231
+ # compiles itself, so only folded classes are recorded here. String
232
+ # classes stay literal and symbol classes stay scoped, matching how
233
+ # dynamic class values render.
234
+ def folded_class_names_from_old_attributes(source)
235
+ return [[], []] unless parse_static_hash(source)
228
236
 
229
- value = static_string_literal_value(parsed.fetch("class"))
230
- value ? value.split : []
231
- end
237
+ parsed = ::Haml::AttributeParser.parse(source)
238
+ return [[], []] unless parsed&.key?("class")
232
239
 
233
- def static_string_literal_value(source)
234
- case Ripper.sexp(source)
240
+ case Ripper.sexp(parsed.fetch("class"))
235
241
  in [:program, [[:string_literal, [:string_content, [:@tstring_content, String => value, _location]]]]]
236
- value
242
+ [[], value.split]
243
+ in [:program, [[:symbol_literal, [:symbol, [_type, String => name, _location]]]]]
244
+ [[name], []]
237
245
  else
238
- nil
246
+ [[], []]
239
247
  end
240
248
  end
241
249
  end
@@ -203,10 +203,6 @@ module Klenod
203
203
  Fragment.new(literal_source(value))
204
204
  end
205
205
 
206
- def frozen_literal(value)
207
- Fragment.new(frozen_literal_source(value))
208
- end
209
-
210
206
  def import_call(dependency_id)
211
207
  Fragment.new("__klenod_import__(#{literal_source(dependency_id)})")
212
208
  end
@@ -443,13 +439,16 @@ module Klenod
443
439
  # Whether the script opens a literal block for its Haml children.
444
440
  # Calls, `super`, and zsuper can take one; lambdas cannot.
445
441
  def block_script?(source)
446
- node = parse_expression(fix_syntax_by_adding_missing_pairs(source), context: :block_script)
447
-
448
- node.respond_to?(:block) && node.block.is_a?(Prism::BlockNode)
442
+ !script_block_node(source).nil?
449
443
  end
450
444
 
451
445
  private
452
446
 
447
+ def script_block_node(source)
448
+ node = parse_expression(fix_syntax_by_adding_missing_pairs(source), context: :block_script)
449
+ node.block if node.respond_to?(:block) && node.block.is_a?(Prism::BlockNode)
450
+ end
451
+
453
452
  def rewrite_ruby_source(source, line_no)
454
453
  source = rewrite_line_constant(source, line_no)
455
454
  rewrite_variables(source)
@@ -580,7 +579,9 @@ module Klenod
580
579
  def block_source(source, body)
581
580
  body_source = to_source(body)
582
581
 
583
- if source.include?("{") && !source.end_with?(" do")
582
+ # Hash arguments, strings, and parameter defaults can contain
583
+ # braces without opening the block that owns the Haml children.
584
+ if script_block_node(source)&.opening_loc&.slice == "{"
584
585
  "#{source} #{body_source} }"
585
586
  else
586
587
  [source, indent(body_source, 2), "end"].join("\n")
@@ -630,17 +631,6 @@ module Klenod
630
631
  "#{symbol_source(name)} =>"
631
632
  end
632
633
 
633
- def frozen_literal_source(value)
634
- case value
635
- when Hash
636
- "{#{value.map { |key, child| "#{literal_source(key)} => #{frozen_literal_source(child)}" }.join(", ")}}.freeze"
637
- when Array
638
- "[#{value.map { |child| frozen_literal_source(child) }.join(", ")}].freeze"
639
- else
640
- literal_source(value)
641
- end
642
- end
643
-
644
634
  def literal_source(value)
645
635
  case value
646
636
  when String
@@ -6,6 +6,7 @@ require_relative "../plugin"
6
6
  require_relative "../dependency"
7
7
  require_relative "../module_id"
8
8
  require_relative "../ruby_import_rewriter"
9
+ require_relative "../ruby_literal"
9
10
  require_relative "../transform_result"
10
11
  require_relative "../watched_pattern"
11
12
  require_relative "intl_plugin"
@@ -176,7 +177,7 @@ module Klenod
176
177
  styles_source = styles_source_for(builder, style_dependencies, class_names_runtime_dependency: class_names_runtime_dependency)
177
178
  haml_helper_dependency = haml_helper_dependency(module_id)
178
179
  dependencies << haml_helper_dependency
179
- translations_source = builder.frozen_literal(translations_for(context, module_id)).source
180
+ translations_source = RubyLiteral.frozen(translations_for(context, module_id))
180
181
  component_class_name = component_class_name(module_id)
181
182
  import_rewriter =
182
183
  lambda do |source, source_line_offset: 0, source_column_offset: 0|
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+
5
+ require_relative "../hashing"
6
+
7
+ module Klenod
8
+ module Build
9
+ module Plugins
10
+ # Keeps the Ruby that a Markdown file compiles to on disk between runs.
11
+ # Compiling is most of the cost of loading a Markdown module, and the
12
+ # result depends only on the text and the plugin's options, so a file
13
+ # that has not changed is not compiled again.
14
+ #
15
+ # There is one entry per name, and a new key replaces it, so the
16
+ # directory does not grow as a file is edited.
17
+ class MarkdownCache
18
+ PARSER_GEMS = %w[kramdown kramdown-parser-gfm].freeze
19
+
20
+ # Everything that decides the result apart from the inputs: the code
21
+ # of this gem and the parser it runs. An edit to either makes every
22
+ # entry stale.
23
+ def self.compiler_key
24
+ @compiler_key ||=
25
+ Hashing.hexdigest(
26
+ [
27
+ *Dir[File.join(__dir__, "..", "**", "*.rb")].sort.map { Hashing.file_hexdigest(it) },
28
+ *PARSER_GEMS.map { Gem.loaded_specs[it]&.version.to_s }
29
+ ].join("\0")
30
+ )
31
+ end
32
+
33
+ def initialize(path)
34
+ @path = path.to_s
35
+ end
36
+
37
+ # `name` says which entry, `inputs` are the strings the result is
38
+ # compiled from. The block compiles them when the entry is missing or
39
+ # was written for other inputs.
40
+ def fetch(name, *inputs)
41
+ key = Hashing.hexdigest([self.class.compiler_key, *inputs].join("\0"))
42
+ path = File.join(@path, "#{Hashing.hexdigest(name)}.rb")
43
+
44
+ read(path, key) || write(path, key, yield)
45
+ end
46
+
47
+ private
48
+
49
+ def read(path, key)
50
+ stored_key, compiled = File.read(path, encoding: Encoding::UTF_8).split("\n", 2)
51
+ compiled if stored_key == key
52
+ rescue SystemCallError
53
+ nil
54
+ end
55
+
56
+ def write(path, key, compiled)
57
+ temp_path = "#{path}.tmp.#{$$}.#{object_id}"
58
+ FileUtils.mkdir_p(@path)
59
+ File.write(temp_path, "#{key}\n#{compiled}")
60
+ File.rename(temp_path, path)
61
+ compiled
62
+ rescue
63
+ FileUtils.rm_f(temp_path) if temp_path
64
+ raise
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -14,6 +14,7 @@ require_relative "class_names_runtime"
14
14
  require_relative "component_defaults"
15
15
  require_relative "data_plugin"
16
16
  require_relative "haml_plugin"
17
+ require_relative "markdown_cache"
17
18
  require_relative "markdown_compiler"
18
19
 
19
20
  module Klenod
@@ -58,10 +59,12 @@ module Klenod
58
59
 
59
60
  def initialize(
60
61
  component_base_class: DEFAULT_COMPONENT_BASE_CLASS,
61
- factory: DEFAULT_FACTORY
62
+ factory: DEFAULT_FACTORY,
63
+ cache_path: nil
62
64
  )
63
65
  @component_base_class = component_base_class
64
66
  @factory = factory
67
+ @cache = cache_path && MarkdownCache.new(cache_path)
65
68
  end
66
69
 
67
70
  def resolve(dependency, _context)
@@ -76,22 +79,9 @@ module Klenod
76
79
  return super unless module_id.extname == ".md"
77
80
 
78
81
  frontmatter, markdown_source = parse_frontmatter(code, module_id)
79
- builder = HamlPlugin::Transformer::RubyBuilder.new(profiler: context.profiler)
80
82
  dependency = markdown_components_dependency(module_id, context)
81
83
  class_names_dependency = class_names_runtime_dependency(module_id)
82
- components_source = components_source_for(dependency)
83
- render_source = MarkdownCompiler.new(factory: @factory, components_source: components_source).compile(markdown_source)
84
- component_class_name = component_class_name(module_id)
85
- class_names_source = "#{builder.import_call(class_names_dependency.id).source}.new({}.freeze)"
86
- generated =
87
- builder.component_source(
88
- component_class_name: component_class_name,
89
- component_base_class: @component_base_class,
90
- translations_source: "{}.freeze",
91
- ruby_source: "",
92
- render_source: render_source,
93
- styles_source: class_names_source
94
- )
84
+ generated = component_source_for(module_id, markdown_source, dependency, class_names_dependency, context)
95
85
  generated = "#{generated}\nDefault.const_set(:Frontmatter, #{frontmatter.inspect}.freeze)\n"
96
86
 
97
87
  TransformResult.new(
@@ -122,6 +112,34 @@ module Klenod
122
112
 
123
113
  private
124
114
 
115
+ # The cache entry is named after the file, so each file has one.
116
+ def component_source_for(module_id, markdown_source, dependency, class_names_dependency, context)
117
+ inputs = [
118
+ component_class_name(module_id),
119
+ components_source_for(dependency),
120
+ class_names_dependency.id,
121
+ markdown_source
122
+ ]
123
+ return compile_component(*inputs, context) unless @cache
124
+
125
+ @cache.fetch(context.absolute_path(module_id).to_s, @factory, @component_base_class, *inputs) do
126
+ compile_component(*inputs, context)
127
+ end
128
+ end
129
+
130
+ def compile_component(component_class_name, components_source, class_names_id, markdown_source, context)
131
+ builder = HamlPlugin::Transformer::RubyBuilder.new(profiler: context.profiler)
132
+
133
+ builder.component_source(
134
+ component_class_name: component_class_name,
135
+ component_base_class: @component_base_class,
136
+ translations_source: "{}.freeze",
137
+ ruby_source: "",
138
+ render_source: MarkdownCompiler.new(factory: @factory, components_source: components_source).compile(markdown_source),
139
+ styles_source: "#{builder.import_call(class_names_id).source}.new({}.freeze)"
140
+ )
141
+ end
142
+
125
143
  def parse_frontmatter(source, module_id)
126
144
  match = source.match(/\A---[ \t]*\r?\n(?<frontmatter>.*?\r?\n)---[ \t]*(?:\r?\n|\z)/m)
127
145
  return [{}, source] unless match
@@ -4,6 +4,7 @@ require_relative "../dependency"
4
4
  require_relative "../errors"
5
5
  require_relative "../module_id"
6
6
  require_relative "../plugin"
7
+ require_relative "../ruby_literal"
7
8
  require "klenod/runtime/source_map"
8
9
  require_relative "../transform_result"
9
10
  require_relative "../watched_pattern"
@@ -376,11 +377,17 @@ module Klenod
376
377
  def generate_router_source(manifest)
377
378
  imports = import_refs(manifest)
378
379
  <<~RUBY
380
+ require "uri"
381
+
379
382
  Segment = Data.define(:name, :kind, :param_name, :path_part)
380
383
  Param = Data.define(:name, :kind)
381
384
  #{import_definitions(imports)}
382
385
 
383
386
  module Default
387
+ # Raised for a request path with a malformed percent-encoding
388
+ # or an escape that does not decode to UTF-8.
389
+ MalformedPathError = Class.new(StandardError)
390
+
384
391
  Route = Data.define(:path, :page_module_id, :handler_module_id, :segments, :match_parts, :layout_module_ids, :slot_layout_module_id, :page_ref, :handler_ref, :layout_refs) do
385
392
  def params
386
393
  segments
@@ -528,7 +535,7 @@ module Klenod
528
535
  end
529
536
 
530
537
  def self.match(path)
531
- parts = normalize_path(path)
538
+ parts = request_parts(path)
532
539
  route = main_route_for(parts)
533
540
  return nil unless route
534
541
 
@@ -553,6 +560,22 @@ module Klenod
553
560
  normalized.split("/").reject(&:empty?)
554
561
  end
555
562
 
563
+ # Request path segments, each percent-decoded after splitting so
564
+ # an encoded slash stays inside its segment. `.` and `..` are
565
+ # kept as ordinary segments; nothing is normalized.
566
+ def self.request_parts(path)
567
+ normalize_path(path).map do |part|
568
+ decoded = begin
569
+ URI.decode_uri_component(part)
570
+ rescue ArgumentError
571
+ nil
572
+ end
573
+ raise MalformedPathError, "Malformed percent-encoding in path segment \#{part.inspect}" unless decoded&.valid_encoding?
574
+
575
+ decoded
576
+ end
577
+ end
578
+
556
579
  def self.build_tree(routes)
557
580
  root = RouteNode.new(segment: nil, path: "/")
558
581
 
@@ -632,7 +655,7 @@ module Klenod
632
655
  end
633
656
 
634
657
  def self.special_match(views, path)
635
- parts = normalize_path(path)
658
+ parts = request_parts(path)
636
659
  view = special_view_for(views, parts)
637
660
  return nil unless view
638
661
 
@@ -742,7 +765,7 @@ module Klenod
742
765
  " #{route.handler_module_id&.path.inspect},",
743
766
  " #{segments_source(route.segments)},",
744
767
  " #{match_parts_source(route.segments)},",
745
- " #{route.layout_module_ids.map(&:path).inspect},",
768
+ " #{RubyLiteral.frozen(route.layout_module_ids.map(&:path))},",
746
769
  " #{route.slot_layout_module_id&.path.inspect},",
747
770
  " #{route.page_module_id ? imports.fetch(route.page_module_id.to_s) : "nil"},",
748
771
  " #{route.handler_module_id ? imports.fetch(route.handler_module_id.to_s) : "nil"},",
@@ -763,7 +786,7 @@ module Klenod
763
786
  " #{view.view_module_id.path.inspect},",
764
787
  " #{segments_source(view.segments)},",
765
788
  " #{match_parts_source(view.segments)},",
766
- " #{view.layout_module_ids.map(&:path).inspect},",
789
+ " #{RubyLiteral.frozen(view.layout_module_ids.map(&:path))},",
767
790
  " #{imports.fetch(view.view_module_id.to_s)},",
768
791
  " #{layouts_source(layouts)}",
769
792
  " )"
@@ -841,11 +864,11 @@ module Klenod
841
864
  end
842
865
 
843
866
  def segments_source(segments)
844
- "[#{segments.map { |segment| segment_source(segment) }.join(", ")}]"
867
+ "[#{segments.map { |segment| segment_source(segment) }.join(", ")}].freeze"
845
868
  end
846
869
 
847
870
  def match_parts_source(segments)
848
- route_match_parts_for(segments).inspect
871
+ RubyLiteral.frozen(route_match_parts_for(segments))
849
872
  end
850
873
 
851
874
  def segment_source(segment)
@@ -883,7 +906,7 @@ module Klenod
883
906
  end
884
907
 
885
908
  def layouts_source(layouts)
886
- "[#{layouts.join(", ")}]"
909
+ "[#{layouts.join(", ")}].freeze"
887
910
  end
888
911
 
889
912
  def route_handler_module_id?(module_id, context)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "date"
4
+
5
+ module Klenod
6
+ module Build
7
+ # Writes parsed data as Ruby source for generated modules. Every hash,
8
+ # array, time, and date is frozen; string literals are frozen because
9
+ # modules are evaluated with `frozen_string_literal: true`.
10
+ module RubyLiteral
11
+ module_function
12
+
13
+ # `inspect` is not Ruby source for every parsed value: dates, times,
14
+ # and non-finite floats need constructor calls instead.
15
+ def frozen(value)
16
+ case value
17
+ when Hash
18
+ entries = value.map { |key, entry| "#{frozen(key)} => #{frozen(entry)}" }
19
+ "{#{entries.join(", ")}}.freeze"
20
+ when Array
21
+ "[#{value.map { frozen(it) }.join(", ")}].freeze"
22
+ when Float
23
+ float(value)
24
+ when Time
25
+ zone = value.utc? ? "UTC" : value.utc_offset
26
+ "::Time.at(#{value.to_i}, #{value.nsec}, :nsec, in: #{zone.inspect}).freeze"
27
+ when Date
28
+ "::Date.new(#{value.year}, #{value.month}, #{value.day}).freeze"
29
+ when nil, true, false, String, Symbol, Integer
30
+ value.inspect
31
+ else
32
+ raise TypeError, "Cannot write #{value.class} as Ruby literal source"
33
+ end
34
+ end
35
+
36
+ def float(value)
37
+ if value.nan?
38
+ "::Float::NAN"
39
+ elsif value.infinite?
40
+ value.positive? ? "::Float::INFINITY" : "-::Float::INFINITY"
41
+ else
42
+ value.inspect
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Klenod
4
4
  module Build
5
- VERSION = "0.0.32"
5
+ VERSION = "0.0.34"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: klenod-build
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.32
4
+ version: 0.0.34
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrés Alin
@@ -15,14 +15,14 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.0.32
18
+ version: 0.0.34
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.0.32
25
+ version: 0.0.34
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: async
28
28
  requirement: !ruby/object:Gem::Requirement
@@ -159,16 +159,22 @@ dependencies:
159
159
  name: prism
160
160
  requirement: !ruby/object:Gem::Requirement
161
161
  requirements:
162
- - - "~>"
162
+ - - ">="
163
163
  - !ruby/object:Gem::Version
164
164
  version: '1.9'
165
+ - - "<"
166
+ - !ruby/object:Gem::Version
167
+ version: '3.0'
165
168
  type: :runtime
166
169
  prerelease: false
167
170
  version_requirements: !ruby/object:Gem::Requirement
168
171
  requirements:
169
- - - "~>"
172
+ - - ">="
170
173
  - !ruby/object:Gem::Version
171
174
  version: '1.9'
175
+ - - "<"
176
+ - !ruby/object:Gem::Version
177
+ version: '3.0'
172
178
  - !ruby/object:Gem::Dependency
173
179
  name: protocol-url
174
180
  requirement: !ruby/object:Gem::Requirement
@@ -288,6 +294,7 @@ files:
288
294
  - lib/klenod/build/plugins/haml_plugin/transformer/ruby_builder.rb
289
295
  - lib/klenod/build/plugins/image_plugin.rb
290
296
  - lib/klenod/build/plugins/intl_plugin.rb
297
+ - lib/klenod/build/plugins/markdown_cache.rb
291
298
  - lib/klenod/build/plugins/markdown_compiler.rb
292
299
  - lib/klenod/build/plugins/markdown_plugin.rb
293
300
  - lib/klenod/build/plugins/router_plugin.rb
@@ -299,6 +306,7 @@ files:
299
306
  - lib/klenod/build/resolution_error_formatter.rb
300
307
  - lib/klenod/build/resolver.rb
301
308
  - lib/klenod/build/ruby_import_rewriter.rb
309
+ - lib/klenod/build/ruby_literal.rb
302
310
  - lib/klenod/build/source_error.rb
303
311
  - lib/klenod/build/source_excerpt.rb
304
312
  - lib/klenod/build/source_map.rb