htx 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +1 -1
  3. data/lib/htx.rb +26 -11
  4. metadata +18 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad1aaabd0904124ebc249af10fd5e8e93c11009f0fc0f7cb31f2f8656d4bb47d
4
- data.tar.gz: 44e8808f07d173efc4830f07e0732822715e9837764518b780efe300ff08f2e6
3
+ metadata.gz: 637943258561910b726a2956a9c9e683b1fe2180ab80803a484032ba56fb3101
4
+ data.tar.gz: b4c6faf31037d5a8e0550e2331e6820d21349e9ca80bed18f9a6611a8004de5b
5
5
  SHA512:
6
- metadata.gz: 5c7533d1b88bfc3365794cd18b8f26083364a060d4fdc1d4d1b3cef6dcb13af87e2ce4beb06553846da8add2481bda8fae5666a614b1c0ffff8b97d2b5aadd9d
7
- data.tar.gz: ef37756cd45b5e7c58ababb1b9350467748816b998f50976055f2541356d282bd15bad20d47325f4ba4a32af1256b7ca407293b45ec496ba23fab494fd4c9f31
6
+ metadata.gz: 82e119f2932d7e96c17a2a336c6bcb88a27759d6657308ac91c4f1e0b6bd4c38000f5e4e45592b2603f6e9f71e6f1d670a4458745b172b157aa429a843d32970
7
+ data.tar.gz: b2583375f80f0062ea9f5a73df2c72c34b728cf3e2c7b863fc4feb93e18e94aaf81a689d45933bcac3914f71b86ce16865cecec8cca86ef728d8a038c2b15eba
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2019-2020 Nate Pickens
1
+ Copyright 2019-2021 Nate Pickens
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
4
4
  documentation files (the "Software"), to deal in the Software without restriction, including without
data/lib/htx.rb CHANGED
@@ -6,21 +6,23 @@ require('nokogiri')
6
6
  # A Ruby compiler for HTX templates.
7
7
  #
8
8
  class HTX
9
- VERSION = '0.0.4'
9
+ VERSION = '0.0.5'
10
10
 
11
11
  CHILDLESS = 0b01
12
12
  TEXT_NODE = 0b10
13
13
  FLAG_BITS = 2
14
14
 
15
+ INDENT_DEFAULT = ' '
15
16
  DYNAMIC_KEY_ATTR = 'htx-key'
16
17
 
17
- LEADING_WHITESPACE = /\A *\n */.freeze
18
- TRAILING_WHITESPACE = /\n *\z/.freeze
19
- NON_BLANK_NON_FIRST_LINE = /(?<=\n) *(?=\S)/.freeze
20
- NEWLINE_NON_BLANK = /\n(?=[^\n]+)/.freeze
18
+ LEADING_WHITESPACE = /\A[ \t]*\n[ \t]*/.freeze
19
+ TRAILING_WHITESPACE = /\n[ \t]*\z/.freeze
20
+ NON_BLANK_NON_FIRST_LINE = /(?<=\n)[ \t]*(?=\S)/.freeze
21
+ NEWLINE_NON_BLANK = /\n(?=[^\n])/.freeze
22
+ INDENT_GUESS = /^[ \t]+/.freeze
21
23
 
22
- END_STATEMENT_END = /(;|\n|\{|\}) *\z/.freeze
23
- BEGIN_STATEMENT_END = /\A *(;|\{|\n|\})/.freeze
24
+ END_STATEMENT_END = /(;|\n|\{|\})[ \t]*\z/.freeze
25
+ BEGIN_STATEMENT_END = /\A[ \t]*(;|\{|\n|\})/.freeze
24
26
  END_WHITESPACE = /\s\z/.freeze
25
27
  BEGIN_WHITESPACE = /\A\s/.freeze
26
28
 
@@ -51,7 +53,11 @@ class HTX
51
53
  end
52
54
 
53
55
  ##
54
- # Compiles the HTX template.
56
+ # Compiles the HTX template. Options:
57
+ #
58
+ # * :indent - Indent output by this number of spaces if Numeric, or by this string if a String (if the
59
+ # latter, may only contain space and tab characters).
60
+ # * :assign_to - Assign the template function to this JavaScript object instead of the `window` object.
55
61
  #
56
62
  def compile(options = EMPTY_HASH)
57
63
  doc = Nokogiri::HTML::DocumentFragment.parse(@template)
@@ -69,12 +75,21 @@ class HTX
69
75
  @compiled = ''.dup
70
76
  @static_key = 0
71
77
 
78
+ @indent =
79
+ if options[:indent].kind_of?(Numeric)
80
+ ' ' * options[:indent]
81
+ elsif options[:indent].kind_of?(String) && options[:indent] !~ /^[ \t]+$/
82
+ raise("Invalid :indent value #{options[:indent].inspect}: only spaces and tabs are allowed")
83
+ else
84
+ options[:indent] || @template[INDENT_GUESS] || INDENT_DEFAULT
85
+ end
86
+
72
87
  process(doc)
73
88
  @compiled.rstrip!
74
89
 
75
90
  <<~EOS
76
91
  #{options[:assign_to] || 'window'}['#{@name}'] = function(htx) {
77
- #{@compiled}
92
+ #{@indent}#{@compiled}
78
93
  }
79
94
  EOS
80
95
  end
@@ -154,7 +169,7 @@ class HTX
154
169
  elsif @compiled !~ END_WHITESPACE && text !~ BEGIN_WHITESPACE
155
170
  @compiled << ' '
156
171
  elsif @compiled[-1] == "\n"
157
- @compiled << ' '
172
+ @compiled << @indent
158
173
  end
159
174
 
160
175
  @compiled << text
@@ -166,7 +181,7 @@ class HTX
166
181
  def indent(text)
167
182
  return '' unless text
168
183
 
169
- text.gsub!(NEWLINE_NON_BLANK, "\n ")
184
+ text.gsub!(NEWLINE_NON_BLANK, "\n#{@indent}")
170
185
  text
171
186
  end
172
187
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nate Pickens
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-01 00:00:00.000000000 Z
11
+ date: 2021-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -42,20 +42,26 @@ dependencies:
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 5.11.2
48
+ - - "<"
46
49
  - !ruby/object:Gem::Version
47
- version: '5.11'
50
+ version: 6.0.0
48
51
  type: :development
49
52
  prerelease: false
50
53
  version_requirements: !ruby/object:Gem::Requirement
51
54
  requirements:
52
- - - "~>"
55
+ - - ">="
53
56
  - !ruby/object:Gem::Version
54
- version: '5.11'
57
+ version: 5.11.2
58
+ - - "<"
59
+ - !ruby/object:Gem::Version
60
+ version: 6.0.0
55
61
  description: HTX is a full-featured HTML template system that is simple, lightweight,
56
62
  and highly performant. This library is a Ruby implementation of the HTX template
57
63
  compiler--it converts HTX templates to their compiled JavaScript form.
58
- email:
64
+ email:
59
65
  executables: []
60
66
  extensions: []
61
67
  extra_rdoc_files: []
@@ -70,7 +76,7 @@ metadata:
70
76
  allowed_push_host: https://rubygems.org
71
77
  homepage_uri: https://github.com/npickens/htx
72
78
  source_code_uri: https://github.com/npickens/htx
73
- post_install_message:
79
+ post_install_message:
74
80
  rdoc_options: []
75
81
  require_paths:
76
82
  - lib
@@ -78,15 +84,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
78
84
  requirements:
79
85
  - - ">="
80
86
  - !ruby/object:Gem::Version
81
- version: '0'
87
+ version: 2.5.8
82
88
  required_rubygems_version: !ruby/object:Gem::Requirement
83
89
  requirements:
84
90
  - - ">="
85
91
  - !ruby/object:Gem::Version
86
92
  version: '0'
87
93
  requirements: []
88
- rubygems_version: 3.1.2
89
- signing_key:
94
+ rubygems_version: 3.2.3
95
+ signing_key:
90
96
  specification_version: 4
91
97
  summary: A Ruby compiler for HTX templates.
92
98
  test_files: []