bidify 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +52 -11
- data/lib/bidify/bidifier.rb +11 -0
- data/lib/bidify/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 32b3c244317bd021ff911b1be914990a87e7056b64d7c7ef8ec08fe4597721d0
|
|
4
|
+
data.tar.gz: db60b88d64e4c288d7160072ffb67edebb46959941294159ab231593ee6d8217
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7ff62ccd19cef7e7427e7a369d97d3832325066e0c73f6ef6ef1fe1774d5895cc3212d82e9df3be6534e614249b0047056483ba4d78cd78bbb9b0648b8134804
|
|
7
|
+
data.tar.gz: 23389f67993355bd021f140b4392b4641800cb01fff36ae0b232a27c70d072c84103f791e83517510ee2c64db4f91765274f2dd4e804d42c10a063b640d81589
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Bidify helps to add bidirectional text support to HTML documents.
|
|
4
4
|
|
|
5
|
-
The project is in its very early stage of development and its interface or
|
|
5
|
+
The project is in its very early stage of development, and its interface or
|
|
6
|
+
functionality may break from one version to another. Use it with caution.
|
|
6
7
|
|
|
7
8
|
## Usage
|
|
8
9
|
|
|
@@ -10,22 +11,20 @@ The project is in its very early stage of development and its interface or funct
|
|
|
10
11
|
require 'bidify'
|
|
11
12
|
|
|
12
13
|
html_input = '<p>some content even in nested format</p>'
|
|
13
|
-
bidified_html = Bidify.
|
|
14
|
+
bidified_html = Bidify.bidify_html_string(html_input)
|
|
14
15
|
# bidified_html: '<p dir="auto">some content even in nested format</p>'
|
|
15
16
|
```
|
|
16
17
|
|
|
17
18
|
## Rules
|
|
18
19
|
|
|
19
|
-
|
|
20
|
+
Bidification (the `dir="auto"` attribute) follows these simple rules:
|
|
20
21
|
|
|
21
|
-
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
22
|
+
- It applies to "bidifiable" tags.
|
|
23
|
+
- It excludes the first immediate child element.
|
|
24
|
+
- It excludes `li` tags.
|
|
24
25
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
Notice that `li` elements shouldn't get `dir=auto` otherwise, the list appearance gets damaged.
|
|
26
|
+
The default bidifiable tags are `div`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`,
|
|
27
|
+
`p`, `ul`, `ol`, and `blockquote`. One can modify this list using options.
|
|
29
28
|
|
|
30
29
|
As a complementary step, CSS styles should use [logical properties](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_logical_properties_and_values). Here are a few examples:
|
|
31
30
|
|
|
@@ -40,6 +39,48 @@ padding-inline-end: 10px;
|
|
|
40
39
|
border-inline-start: 1px;
|
|
41
40
|
```
|
|
42
41
|
|
|
42
|
+
## Configuration
|
|
43
|
+
|
|
44
|
+
To use this gem with custom configuration, use the following syntax and pass
|
|
45
|
+
options while creating an instance of bidifier:
|
|
46
|
+
|
|
47
|
+
```rb
|
|
48
|
+
options = {
|
|
49
|
+
# ...
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
bidifier = Bidify::HtmlStringBidifier.new(options)
|
|
53
|
+
|
|
54
|
+
puts bidifier.apply('<div>input stringified html</div>')
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Options
|
|
58
|
+
|
|
59
|
+
Available options with their default values are as follows:
|
|
60
|
+
|
|
61
|
+
- `excluding_tags: []`
|
|
62
|
+
|
|
63
|
+
Removes tags from the list of bidifiable tags. This option affects the
|
|
64
|
+
provided tags in `including_tags` options.
|
|
65
|
+
|
|
66
|
+
- `including_tags: []`
|
|
67
|
+
|
|
68
|
+
Adds new tags to the list of bidifiable tags
|
|
69
|
+
|
|
70
|
+
- `greedy: false`
|
|
71
|
+
|
|
72
|
+
By default, bidification stops when it reaches an element that has `dir`
|
|
73
|
+
attribute. Use `true` to disregard any existing `dir` attributes.
|
|
74
|
+
|
|
75
|
+
- `only_tags: []`
|
|
76
|
+
|
|
77
|
+
It sets the bidifiable tags to the given tags.
|
|
78
|
+
|
|
79
|
+
- `with_table_support: false`
|
|
80
|
+
|
|
81
|
+
Use `true` to add table tags support.
|
|
82
|
+
|
|
43
83
|
## License
|
|
44
84
|
|
|
45
|
-
This project is a Free/Libre and Open Source software released under LGPLv3
|
|
85
|
+
This project is a Free/Libre and Open Source software released under LGPLv3
|
|
86
|
+
license.
|
data/lib/bidify/bidifier.rb
CHANGED
|
@@ -23,12 +23,17 @@ module Bidify
|
|
|
23
23
|
def configure
|
|
24
24
|
@bidifiable_tags = DEFAULT_BIDIFIABLE_TAGS.dup
|
|
25
25
|
@bidifiable_tags.concat(TABLE_TAGS) if @options[:with_table_support]
|
|
26
|
+
@bidifiable_tags.concat(@options[:including_tags]) if @options.key?(:including_tags)
|
|
27
|
+
@bidifiable_tags.delete_if { |tag| @options[:excluding_tags]&.include?(tag) }
|
|
28
|
+
@bidifiable_tags = @options[:only_tags] if @options.key?(:only_tags)
|
|
26
29
|
end
|
|
27
30
|
|
|
28
31
|
def bidify_recursively(html_node, options = {})
|
|
29
32
|
seen_the_first_bidifiable_element = false
|
|
30
33
|
|
|
31
34
|
html_node.children.each do |child_node|
|
|
35
|
+
next if stop_recursion_at?(child_node)
|
|
36
|
+
|
|
32
37
|
bidify_recursively(child_node)
|
|
33
38
|
|
|
34
39
|
if (options[:root] || seen_the_first_bidifiable_element) && @bidifiable_tags.include?(child_node.name)
|
|
@@ -42,5 +47,11 @@ module Bidify
|
|
|
42
47
|
def actual_content?(node)
|
|
43
48
|
node.element? || (node.text? && !node.blank?)
|
|
44
49
|
end
|
|
50
|
+
|
|
51
|
+
def stop_recursion_at?(node)
|
|
52
|
+
return false if @options[:greedy] == true
|
|
53
|
+
|
|
54
|
+
node.has_attribute?('dir')
|
|
55
|
+
end
|
|
45
56
|
end
|
|
46
57
|
end
|
data/lib/bidify/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bidify
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Mostafa Ahangarha
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2023-07-
|
|
11
|
+
date: 2023-07-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: nokogiri
|