bidify 0.1.1 → 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 +70 -3
- data/lib/bidify/bidifier.rb +57 -0
- data/lib/bidify/html_string_bidifier.rb +31 -0
- data/lib/bidify/version.rb +1 -1
- data/lib/bidify.rb +15 -31
- metadata +4 -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,10 +11,76 @@ 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
|
|
|
18
|
+
## Rules
|
|
19
|
+
|
|
20
|
+
Bidification (the `dir="auto"` attribute) follows these simple rules:
|
|
21
|
+
|
|
22
|
+
- It applies to "bidifiable" tags.
|
|
23
|
+
- It excludes the first immediate child element.
|
|
24
|
+
- It excludes `li` tags.
|
|
25
|
+
|
|
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.
|
|
28
|
+
|
|
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:
|
|
30
|
+
|
|
31
|
+
```css
|
|
32
|
+
/* Physical properties */
|
|
33
|
+
text-align: left;
|
|
34
|
+
padding-right: 10px;
|
|
35
|
+
border-left: 1px;
|
|
36
|
+
/* Logical properties */
|
|
37
|
+
text-align: start;
|
|
38
|
+
padding-inline-end: 10px;
|
|
39
|
+
border-inline-start: 1px;
|
|
40
|
+
```
|
|
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
|
+
|
|
17
83
|
## License
|
|
18
84
|
|
|
19
|
-
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.
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bidify
|
|
4
|
+
# Super class for custom bidifiers
|
|
5
|
+
class Bidifier
|
|
6
|
+
###
|
|
7
|
+
# Instanciate a bidifier object with the given optional options hash
|
|
8
|
+
#
|
|
9
|
+
# @param [Hash] options
|
|
10
|
+
#
|
|
11
|
+
def initialize(options = {})
|
|
12
|
+
@options = options
|
|
13
|
+
|
|
14
|
+
configure
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def apply
|
|
18
|
+
raise NotImplementedError
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def configure
|
|
24
|
+
@bidifiable_tags = DEFAULT_BIDIFIABLE_TAGS.dup
|
|
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)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def bidify_recursively(html_node, options = {})
|
|
32
|
+
seen_the_first_bidifiable_element = false
|
|
33
|
+
|
|
34
|
+
html_node.children.each do |child_node|
|
|
35
|
+
next if stop_recursion_at?(child_node)
|
|
36
|
+
|
|
37
|
+
bidify_recursively(child_node)
|
|
38
|
+
|
|
39
|
+
if (options[:root] || seen_the_first_bidifiable_element) && @bidifiable_tags.include?(child_node.name)
|
|
40
|
+
child_node.set_attribute('dir', 'auto')
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
seen_the_first_bidifiable_element = true if actual_content?(child_node)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def actual_content?(node)
|
|
48
|
+
node.element? || (node.text? && !node.blank?)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def stop_recursion_at?(node)
|
|
52
|
+
return false if @options[:greedy] == true
|
|
53
|
+
|
|
54
|
+
node.has_attribute?('dir')
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'nokogiri'
|
|
4
|
+
require_relative 'bidifier'
|
|
5
|
+
|
|
6
|
+
module Bidify
|
|
7
|
+
###
|
|
8
|
+
# Bidifying html input as string
|
|
9
|
+
#
|
|
10
|
+
# == Example Usage
|
|
11
|
+
#
|
|
12
|
+
# Here's an example of how to use the class:
|
|
13
|
+
#
|
|
14
|
+
# bidifier = HtmlStringBidifier.new
|
|
15
|
+
# bidifier.apply("<p>text</p>")
|
|
16
|
+
#
|
|
17
|
+
class HtmlStringBidifier < Bidifier
|
|
18
|
+
###
|
|
19
|
+
# Applies dir="auto" to the given html string with default configuration
|
|
20
|
+
#
|
|
21
|
+
# @param [String] html_string a stringified HTML content
|
|
22
|
+
# @return [String] Bidified version of the input string
|
|
23
|
+
#
|
|
24
|
+
def apply(html_input)
|
|
25
|
+
html_node = Nokogiri::HTML.fragment(html_input)
|
|
26
|
+
bidify_recursively(html_node, { root: true })
|
|
27
|
+
|
|
28
|
+
html_node.to_s
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/bidify/version.rb
CHANGED
data/lib/bidify.rb
CHANGED
|
@@ -1,43 +1,27 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require '
|
|
3
|
+
require 'bidify/html_string_bidifier'
|
|
4
4
|
|
|
5
|
-
# Bidify
|
|
5
|
+
# == Bidify module
|
|
6
6
|
#
|
|
7
|
-
#
|
|
7
|
+
# The namespace and helper methods for bidiying HTML contents
|
|
8
8
|
#
|
|
9
|
-
#
|
|
9
|
+
# == Example:
|
|
10
|
+
#
|
|
11
|
+
# bidified_html = Bidify.bidify_html_string(regular_html)
|
|
10
12
|
module Bidify
|
|
11
|
-
|
|
13
|
+
DEFAULT_BIDIFIABLE_TAGS = %w[div h1 h2 h3 h4 h5 h6 p ul ol blockquote].freeze
|
|
14
|
+
TABLE_TAGS = %w[table thead tbody th tr td].freeze
|
|
12
15
|
|
|
13
16
|
class << self
|
|
14
17
|
###
|
|
15
|
-
#
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
private
|
|
24
|
-
|
|
25
|
-
def bidify_recursively(html_node, options = {})
|
|
26
|
-
seen_the_first_bidifiable_element = false
|
|
27
|
-
|
|
28
|
-
html_node.children.each do |child_node|
|
|
29
|
-
bidify_recursively(child_node)
|
|
30
|
-
|
|
31
|
-
if (options[:root] || seen_the_first_bidifiable_element) && @bidifiable_tags.include?(child_node.name)
|
|
32
|
-
child_node.set_attribute('dir', 'auto')
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
seen_the_first_bidifiable_element = true if actual_content?(child_node)
|
|
36
|
-
end
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def actual_content?(node)
|
|
40
|
-
node.element? || (node.text? && !node.blank?)
|
|
18
|
+
# Applies dir="auto" to the given html string with default configuration
|
|
19
|
+
#
|
|
20
|
+
# @param [String] html_string a stringified HTML content
|
|
21
|
+
# @return [String] Bidified version of the input string
|
|
22
|
+
#
|
|
23
|
+
def bidify_html_string(html_string)
|
|
24
|
+
Bidify::HtmlStringBidifier.new.apply(html_string)
|
|
41
25
|
end
|
|
42
26
|
end
|
|
43
27
|
end
|
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-
|
|
11
|
+
date: 2023-07-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: nokogiri
|
|
@@ -36,6 +36,8 @@ files:
|
|
|
36
36
|
- LICENSE
|
|
37
37
|
- README.md
|
|
38
38
|
- lib/bidify.rb
|
|
39
|
+
- lib/bidify/bidifier.rb
|
|
40
|
+
- lib/bidify/html_string_bidifier.rb
|
|
39
41
|
- lib/bidify/version.rb
|
|
40
42
|
homepage: https://github.com/dobidi/bidify-rb
|
|
41
43
|
licenses:
|