bidify 0.1.1 → 0.2.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 +26 -0
- data/lib/bidify/bidifier.rb +46 -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: 28b6f2a2a467a1c0c61b7da79e06f2113cce82e0142c1c4646ba07a47261033f
|
|
4
|
+
data.tar.gz: 82ac8d4762aaf7ca746c5e7a3b1d285e85528929c4649c1396f68884b2b4863b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: efa3b752d9cabd3f6a37deb3a6a3a870214d2ccb3ccb6471d754dc99e6402ec389b2e18b44cc8c94ba2602a5c67a11628c72e572bed14f55e3adaf7884c2453a
|
|
7
|
+
data.tar.gz: 68f8d08def9b0a02c3ed7e34d10832bb29cbd918f6c59337585f51acf83a85e4c7d75ea50c89f8285f0d2d3e061f982d060edc0c4c00810a64ae775aa87d0b71
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -14,6 +14,32 @@ bidified_html = Bidify.bidify(html_input)
|
|
|
14
14
|
# bidified_html: '<p dir="auto">some content even in nested format</p>'
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
+
## Rules
|
|
18
|
+
|
|
19
|
+
The `dir="auto"` attribute should be applied on "bidifiable" tags as per the following rules:
|
|
20
|
+
|
|
21
|
+
- Only apply on "bidifiable" tags.
|
|
22
|
+
- All top-level elements
|
|
23
|
+
- All child elements except the first child
|
|
24
|
+
|
|
25
|
+
Bidifiable elements can be defined as per the use case requirements (not yet implemented).
|
|
26
|
+
The default bidifiable tags are: `div`, `h1`, `h2`, `h3`, `h4`, `h5`, `h6`, `p`, `ul`, `ol`, `blockquote`.
|
|
27
|
+
|
|
28
|
+
Notice that `li` elements shouldn't get `dir=auto` otherwise, the list appearance gets damaged.
|
|
29
|
+
|
|
30
|
+
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
|
+
|
|
32
|
+
```css
|
|
33
|
+
/* Physical properties */
|
|
34
|
+
text-align: left;
|
|
35
|
+
padding-right: 10px;
|
|
36
|
+
border-left: 1px;
|
|
37
|
+
/* Logical properties */
|
|
38
|
+
text-align: start;
|
|
39
|
+
padding-inline-end: 10px;
|
|
40
|
+
border-inline-start: 1px;
|
|
41
|
+
```
|
|
42
|
+
|
|
17
43
|
## License
|
|
18
44
|
|
|
19
45
|
This project is a Free/Libre and Open Source software released under LGPLv3 license.
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
end
|
|
27
|
+
|
|
28
|
+
def bidify_recursively(html_node, options = {})
|
|
29
|
+
seen_the_first_bidifiable_element = false
|
|
30
|
+
|
|
31
|
+
html_node.children.each do |child_node|
|
|
32
|
+
bidify_recursively(child_node)
|
|
33
|
+
|
|
34
|
+
if (options[:root] || seen_the_first_bidifiable_element) && @bidifiable_tags.include?(child_node.name)
|
|
35
|
+
child_node.set_attribute('dir', 'auto')
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
seen_the_first_bidifiable_element = true if actual_content?(child_node)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def actual_content?(node)
|
|
43
|
+
node.element? || (node.text? && !node.blank?)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
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.2.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-02 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:
|