wordpress_util 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a34ea7bd94af6b2cad0fd4d4f53bd4960bd6bff3
4
+ data.tar.gz: 4dcd92b3dbbd4e7d6b90485876f27686f80a8e2b
5
+ SHA512:
6
+ metadata.gz: 0f1624cf1d16cce20deaf9d51d1de82ccdbe74ff06396e9cff4fb14e149487fa6f4d2bcddcf0bf15269fdcbed237eddc2e4ed928f258355baf01f47496f0c4a8
7
+ data.tar.gz: 2f3952fe7b9a2a7415fc7ace951f4130ccc4c7b5d201132d3bb791d1c154dc06902e97e662142379762ff84d8e74c3bb141e462939b3111665b3798d66b6b409
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in wordpress_util.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Leonardo Alifraco
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ [![Build Status](https://travis-ci.org/leonardoalifraco/wordpress_util.svg?branch=master)](https://travis-ci.org/leonardoalifraco/wordpress_util)
2
+
3
+ # WordpressUtil
4
+
5
+ Group of util and formatting methods defined in Wordpress and ported to Ruby.
6
+
7
+ This gem includes:
8
+ - wpautop
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'wordpress_util'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ $ bundle
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install wordpress_util
25
+
26
+ ## Usage
27
+
28
+ ### wpautop
29
+
30
+ Replaces double line-breaks with paragraph elements.
31
+ A group of regex replaces used to identify text formatted with newlines and
32
+ replace double line-breaks with HTML paragraph tags. The remaining line-breaks
33
+ after conversion become <br /> tags, unless br is set to '0' or 'false'.
34
+
35
+ ```ruby
36
+ WordpressUtil.wpautop("some text")
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ Bug reports and pull requests are welcome on GitHub at https://github.com/leonardoalifraco/wordpress_util. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
42
+
43
+ ## License
44
+
45
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
46
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wordpress_util"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,191 @@
1
+ require "wordpress_util/version"
2
+
3
+ class WordpressUtil
4
+ class << self
5
+ ##
6
+ # Replaces double line-breaks with paragraph elements.
7
+ #
8
+ # A group of regex replaces used to identify text formatted with newlines and
9
+ # replace double line-breaks with HTML paragraph tags. The remaining line-breaks
10
+ # after conversion become <<br />> tags, unless $br is set to '0' or 'false'.
11
+ #
12
+ # ==== Parameters
13
+ # * string pee The text which has to be formatted.
14
+ # * bool br Optional. If set, this will convert all remaining line-breaks after paragraphing. Default true.
15
+ def wpautop(pee, br = true)
16
+ pre_tags = {}
17
+
18
+ return "" if pee.strip == ""
19
+
20
+ # just to make things a little easier, pad the end
21
+ pee = "#{pee}\n"
22
+
23
+ # Pre tags shouldn"t be touched by autop.
24
+ # Replace pre tags with placeholders and bring them back after autop.
25
+ if pee.index("<pre")
26
+ pee_parts = pee.split("</pre>")
27
+ last_pee = pee_parts.pop
28
+ pee = ""
29
+ i = 0
30
+
31
+ pee_parts.each do |pee_part|
32
+ start = pee_part.index("<pre")
33
+
34
+ # malformed html?
35
+ unless start
36
+ pee += pee_part
37
+ next
38
+ end
39
+
40
+ name = "<pre wp-pre-tag-#{i}></pre>"
41
+ pre_tags[name] = pee_part[start..-1] + "</pre>"
42
+
43
+ pee += pee_part[0..start-1] + name
44
+ i += 1
45
+ end
46
+
47
+ pee += last_pee
48
+ end
49
+
50
+ # Change multiple <br>s into two line breaks, which will turn into paragraphs.
51
+ pee = pee.gsub(/<br\s*\/?>\s*<br\s*\/?>/, "\n\n")
52
+
53
+ allblocks = "(?:table|thead|tfoot|caption|col|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|form|map|area|blockquote|address|math|style|p|h[1-6]|hr|fieldset|legend|section|article|aside|hgroup|header|footer|nav|figure|figcaption|details|menu|summary)"
54
+
55
+ # Add a single line break above block-level opening tags.
56
+ pee = pee.gsub(Regexp.new("(<" + allblocks + "[\\s/>])"), "\n\\1")
57
+
58
+ # Add a double line break below block-level closing tags.
59
+ pee = pee.gsub(Regexp.new("(</" + allblocks + ">)"), "\\1\n\n")
60
+
61
+ # Standardize newline characters to "\n".
62
+ pee = pee.gsub(/\r\n|\r/, "\n")
63
+
64
+ # Find newlines in all elements and add placeholders.
65
+ pee = wp_replace_in_html_tags(pee, { "\n" => " <!-- wpnl --> " });
66
+
67
+ # Collapse line breaks before and after <option> elements so they don"t get autop"d.
68
+ if pee.index("<option")
69
+ pee = pee.gsub(/\s*<option/, "<option")
70
+ pee = pee.gsub(/<\/option>\s*/, "</option>")
71
+ end
72
+
73
+ # Collapse line breaks inside <object> elements, before <param> and <embed> elements
74
+ # so they don't get autop'd.
75
+ if pee.index("</object>")
76
+ pee = pee.gsub(/(<object[^>]*>)\s*/, "\\1")
77
+ pee = pee.gsub(/\s*<\/object>/, '</object>')
78
+ pee = pee.gsub(/\s*(<\/?(?:param|embed)[^>]*>)\s*/, "\\1")
79
+ end
80
+
81
+ # Collapse line breaks inside <audio> and <video> elements,
82
+ # before and after <source> and <track> elements.
83
+ if pee.index("<source") || pee.index("<track")
84
+ pee = pee.gsub(/([<\[](?:audio|video)[^>\]]*[>\]])\s*/, "\\1")
85
+ pee = pee.gsub(/\s*([<\[]\/(?:audio|video)[>\]])/, "\\1")
86
+ pee = pee.gsub(/\s*(<(?:source|track)[^>]*>)\s*/, "\\1")
87
+ end
88
+
89
+ # Remove more than two contiguous line breaks.
90
+ pee = pee.gsub(/\n\n+/, "\n\n")
91
+
92
+ # Split up the contents into an array of strings, separated by double line breaks.
93
+ pees = pee.split(/\n\s*\n/)
94
+
95
+ # Reset pee prior to rebuilding.
96
+ pee = ""
97
+
98
+ # Rebuild the content as a string, wrapping every bit with a <p>.
99
+ pees.each do |tinkle|
100
+ pee += "<p>" + tinkle.strip + "</p>\n"
101
+ end
102
+
103
+ # Under certain strange conditions it could create a P of entirely whitespace.
104
+ pee = pee.gsub(/<p>\s*<\/p>/, "")
105
+
106
+ # Add a closing <p> inside <div>, <address>, or <form> tag if missing.
107
+ pee = pee.gsub(/<p>([^<]+)<\/(div|address|form)>/, "<p>\\1</p></\\2>")
108
+
109
+ # If an opening or closing block element tag is wrapped in a <p>, unwrap it.
110
+ pee = pee.gsub(Regexp.new("<p>\\s*(</?" + allblocks + "[^>]*>)\\s*</p>"), "\\1")
111
+
112
+ # In some cases <li> may get wrapped in <p>, fix them.
113
+ pee = pee.gsub(/<p>(<li.+?)<\/p>/, "\\1")
114
+
115
+ # If a <blockquote> is wrapped with a <p>, move it inside the <blockquote>.
116
+ pee = pee.gsub(/<p><blockquote([^>]*)>/i, "<blockquote\\1><p>")
117
+ pee = pee.gsub("</blockquote></p>", "</p></blockquote>")
118
+
119
+ # If an opening or closing block element tag is preceded by an opening <p> tag, remove it.
120
+ pee = pee.gsub(Regexp.new("<p>\\s*(</?" + allblocks + "[^>]*>)"), "\\1")
121
+
122
+ # If an opening or closing block element tag is followed by a closing <p> tag, remove it.
123
+ pee = pee.gsub(Regexp.new("(</?" + allblocks + "[^>]*>)\\s*</p>"), "\\1")
124
+
125
+ # Optionally insert line breaks.
126
+ if br
127
+ pee = pee.gsub(Regexp.new("<(script|style).*?<\/\\1>")) do |s|
128
+ s.gsub("\n", "<WPPreserveNewline \/>")
129
+ end
130
+
131
+ # Normalize <br>
132
+ pee = pee.gsub(/<br>|<br\/>/, "<br />")
133
+
134
+ # Replace any new line characters that aren"t preceded by a <br /> with a <br />.
135
+ pee = pee.gsub(/(?<!<br \/>)\s*\n/, "<br />\n")
136
+
137
+ pee = pee.gsub("<WPPreserveNewline />", "\n")
138
+ end
139
+
140
+ # If a <br /> tag is after an opening or closing block tag, remove it.
141
+ pee = pee.gsub(Regexp.new("(</?"+allblocks+"[^>]*>)\s*<br />"), "\\1")
142
+
143
+ # If a <br /> tag is before a subset of opening or closing block tags, remove it.
144
+ pee = pee.gsub(/<br \/>(\s*<\/?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)/, "\\1")
145
+ pee = pee.gsub(/\n<\/p>$/, "</p>")
146
+
147
+ # Replace placeholder <pre> tags with their original content.
148
+ if pre_tags.any?
149
+ pre_tags.each { |key, value| pee = pee.gsub(key, value) }
150
+ end
151
+
152
+ # Restore newlines in all elements.
153
+ if pee.index("<!-- wpnl -->")
154
+ [" <!-- wpnl --> ", "<!-- wpnl -->"].each { |search| pee = pee.gsub(search, "\n") }
155
+ end
156
+
157
+ return pee
158
+ end
159
+
160
+ private
161
+ def wp_replace_in_html_tags(haystack, replace_pairs)
162
+ # Find all elements
163
+ textarr = wp_html_split(haystack)
164
+ changed = false
165
+
166
+ # Replace the pairs
167
+ replace_pairs.each do |needle, replace|
168
+ textarr.each_with_index do |element, index|
169
+ if index.odd? && textarr[index].index(needle)
170
+ textarr[index] = textarr[index].gsub(needle, replace)
171
+ changed = true
172
+ end
173
+ end
174
+ end
175
+
176
+ return textarr.join if changed
177
+ haystack
178
+ end
179
+
180
+ def wp_html_split(input)
181
+ input.split(Regexp.new(get_html_split_regex))
182
+ end
183
+
184
+ def get_html_split_regex
185
+ comments = "!(?:-(?!->)[^\\-]*+)*+(?:-->)?"
186
+ cdata = "!\\[CDATA\\[[^\\]]*+(?:\\](?!\\]>)[^\\]]*+)*+(?:\\]\\]>)?"
187
+ regex = "(<(?=(!--|!\\[CDATA\\[))?(?(2)(?=(!-))?(?(3)" + comments + "|" + cdata + ")|[^>]*>?))"
188
+ return regex
189
+ end
190
+ end
191
+ end
@@ -0,0 +1,3 @@
1
+ class WordpressUtil
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wordpress_util/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wordpress_util"
8
+ spec.version = WordpressUtil::VERSION
9
+ spec.authors = ["Leonardo Alifraco"]
10
+ spec.email = ["leonardo.alifraco@gmail.com"]
11
+
12
+ spec.summary = "Group of util and formatting methods defined in Wordpress and ported to Ruby"
13
+ spec.description = "Group of util and formatting methods defined in Wordpress and ported to Ruby"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wordpress_util
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Leonardo Alifraco
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Group of util and formatting methods defined in Wordpress and ported
56
+ to Ruby
57
+ email:
58
+ - leonardo.alifraco@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/wordpress_util.rb
73
+ - lib/wordpress_util/version.rb
74
+ - wordpress_util.gemspec
75
+ homepage:
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.5.1
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: Group of util and formatting methods defined in Wordpress and ported to Ruby
99
+ test_files: []