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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28b6f2a2a467a1c0c61b7da79e06f2113cce82e0142c1c4646ba07a47261033f
4
- data.tar.gz: 82ac8d4762aaf7ca746c5e7a3b1d285e85528929c4649c1396f68884b2b4863b
3
+ metadata.gz: 32b3c244317bd021ff911b1be914990a87e7056b64d7c7ef8ec08fe4597721d0
4
+ data.tar.gz: db60b88d64e4c288d7160072ffb67edebb46959941294159ab231593ee6d8217
5
5
  SHA512:
6
- metadata.gz: efa3b752d9cabd3f6a37deb3a6a3a870214d2ccb3ccb6471d754dc99e6402ec389b2e18b44cc8c94ba2602a5c67a11628c72e572bed14f55e3adaf7884c2453a
7
- data.tar.gz: 68f8d08def9b0a02c3ed7e34d10832bb29cbd918f6c59337585f51acf83a85e4c7d75ea50c89f8285f0d2d3e061f982d060edc0c4c00810a64ae775aa87d0b71
6
+ metadata.gz: 7ff62ccd19cef7e7427e7a369d97d3832325066e0c73f6ef6ef1fe1774d5895cc3212d82e9df3be6534e614249b0047056483ba4d78cd78bbb9b0648b8134804
7
+ data.tar.gz: 23389f67993355bd021f140b4392b4641800cb01fff36ae0b232a27c70d072c84103f791e83517510ee2c64db4f91765274f2dd4e804d42c10a063b640d81589
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bidify (0.1.1)
4
+ bidify (0.2.0)
5
5
  nokogiri (~> 1.14)
6
6
 
7
7
  GEM
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 functionality may break from one version to another. Use it with caution.
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.bidify(html_input)
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
- The `dir="auto"` attribute should be applied on "bidifiable" tags as per the following rules:
20
+ Bidification (the `dir="auto"` attribute) follows these simple rules:
20
21
 
21
- - Only apply on "bidifiable" tags.
22
- - All top-level elements
23
- - All child elements except the first child
22
+ - It applies to "bidifiable" tags.
23
+ - It excludes the first immediate child element.
24
+ - It excludes `li` tags.
24
25
 
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.
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 license.
85
+ This project is a Free/Libre and Open Source software released under LGPLv3
86
+ license.
@@ -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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bidify
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  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.2.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-02 00:00:00.000000000 Z
11
+ date: 2023-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri