html-pipeline-ruby_markup 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +1 -0
- data/CHANGELOG.md +9 -0
- data/CONTRIBUTING.md +13 -0
- data/Gemfile +14 -0
- data/LICENSE.md +21 -0
- data/README.md +55 -0
- data/Rakefile +9 -0
- data/html-pipeline-ruby_markup.gemspec +20 -0
- data/lib/html/pipeline/ruby_markup.rb +16 -0
- data/lib/html/pipeline/ruby_markup/element.rb +34 -0
- data/lib/html/pipeline/ruby_markup/markdown_traverser.rb +45 -0
- data/lib/html/pipeline/ruby_markup/transformer.rb +59 -0
- data/lib/html/pipeline/ruby_markup/version.rb +9 -0
- metadata +71 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 67c4ec7e23acac58f3008b76c0b09953cacdd9b37d6817b0d4239ecab82dd02a
|
4
|
+
data.tar.gz: 0a4e5736b4a58898b63714f2e8eef0a4e5185a167c8effda2d9d99665b7db373
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8d8c1340bd142e7962978fd1438a96956a7698389e448aea60c8c89255a1b7ba0b8122b25fcc1fa7d5ae081cddc9193b6436bad003d1edc6a392f92e8ddf1f65
|
7
|
+
data.tar.gz: 329e07d8356e3fc55938ecb33e08502c60a6181961a4166067fe9096853741c938f436635a50137082664c1ebf9ea6d5f77b82032274e021fb2bade143d5e4aa
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/CHANGELOG.md
ADDED
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributing
|
2
|
+
|
3
|
+
1. [Fork it](https://github.com/juanitofatas/html-pipeline-ruby_markup/fork)
|
4
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
5
|
+
3. Commit your changes (`git commit -am "Add some feature"`)
|
6
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
7
|
+
5. Create a new Pull Request
|
8
|
+
|
9
|
+
## Running tests
|
10
|
+
|
11
|
+
```shell
|
12
|
+
$ rake
|
13
|
+
```
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Juanito Fatas
|
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,55 @@
|
|
1
|
+
# HTML::Pipeline::RubyMarkup
|
2
|
+
|
3
|
+
`HTML::Pipeline::RubyMarkup` provides a [HTML::Pipeline](https://github.com/jch/html-pipeline)
|
4
|
+
filter to easily write [ruby markups] in Markdown documents.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application‘s `Gemfile`:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem "html-pipeline-ruby_markup"
|
12
|
+
```
|
13
|
+
|
14
|
+
Or install it yourself as:
|
15
|
+
|
16
|
+
$ gem install html-pipeline-ruby_markup
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
`HTML::Pipeline::RubyMarkup` processes Text, so it **must** come before the
|
21
|
+
markdown filter.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require "html/pipeline"
|
25
|
+
require "html/pipeline/ruby_markup"
|
26
|
+
|
27
|
+
pipeline = HTML::Pipeline.new [
|
28
|
+
HTML::Pipeline::RubyMarkup::Filter,
|
29
|
+
HTML::Pipeline::MarkdownFilter,
|
30
|
+
]
|
31
|
+
|
32
|
+
result = pipeline.call <<-MARKDOWN.strip_heredoc
|
33
|
+
[漢字(かんじ)]
|
34
|
+
MARKDOWN
|
35
|
+
|
36
|
+
puts result[:output].to_html
|
37
|
+
```
|
38
|
+
|
39
|
+
prints:
|
40
|
+
|
41
|
+
```html
|
42
|
+
<ruby>
|
43
|
+
漢字<rp>(</rp><rt>かんじ</rt><rp>)</rp>
|
44
|
+
</ruby>
|
45
|
+
```
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
Please see the [CONTRIBUTING.md](/CONTRIBUTING.md) file.
|
50
|
+
|
51
|
+
## License
|
52
|
+
|
53
|
+
Please see the [LICENSE.md](/LICENSE.md) file.
|
54
|
+
|
55
|
+
[ruby markups]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ruby
|
data/Rakefile
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "html/pipeline/ruby_markup/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "html-pipeline-ruby_markup"
|
8
|
+
spec.version = HTML::Pipeline::RubyMarkup::VERSION
|
9
|
+
spec.authors = ["Juanito Fatas"]
|
10
|
+
spec.email = ["katehuang0320@gmail.com"]
|
11
|
+
spec.summary = %q{A HTML::Pipeline filter to write ruby markup elements.}
|
12
|
+
spec.description = spec.summary
|
13
|
+
spec.homepage = "https://github.com/juanitofatas/html-pipeline-ruby_markup"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f =~ %r{^(spec)/} }
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.add_dependency "html-pipeline", ">= 1.11"
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "html/pipeline"
|
4
|
+
require_relative "ruby_markup/transformer"
|
5
|
+
|
6
|
+
module HTML
|
7
|
+
class Pipeline
|
8
|
+
module RubyMarkup
|
9
|
+
class Filter < HTML::Pipeline::TextFilter
|
10
|
+
def call
|
11
|
+
Transformer.run(text.dup)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HTML
|
4
|
+
class Pipeline
|
5
|
+
module RubyMarkup
|
6
|
+
class Element
|
7
|
+
def initialize(word, reading, uri)
|
8
|
+
@word = word
|
9
|
+
@reading = reading
|
10
|
+
@uri = uri
|
11
|
+
end
|
12
|
+
|
13
|
+
def original
|
14
|
+
if uri
|
15
|
+
%([#{word}(#{reading})](#{uri}))
|
16
|
+
else
|
17
|
+
%([#{word}(#{reading})])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_html
|
22
|
+
if uri
|
23
|
+
%(<ruby><a href="#{uri}" target="_blank" rel="noopener noreferrer" itemprop="url" aria-label="search #{word} on jisho.org">#{word}</a><rt>#{reading}</rt></ruby>)
|
24
|
+
else
|
25
|
+
"<ruby>#{word}<rp>(</rp><rt>#{reading}</rt><rp>)</rp></ruby>"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
attr_reader :word, :reading, :uri
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module HTML
|
4
|
+
class Pipeline
|
5
|
+
module RubyMarkup
|
6
|
+
# To go through a markdown document. Mainly used to decide if we are in
|
7
|
+
# code blocks to decide if we should escape underscored usernames.
|
8
|
+
class MarkdownTraverser
|
9
|
+
def initialize(markdown)
|
10
|
+
@lines = markdown.dup.lines
|
11
|
+
init_position
|
12
|
+
end
|
13
|
+
|
14
|
+
def each
|
15
|
+
lines.each do |line|
|
16
|
+
update_position(line.include?(CODEBLOCK_MARKER))
|
17
|
+
yield(line)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def in_codeblock?
|
22
|
+
prev == true && current == false
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
CODEBLOCK_MARKER = "```".freeze
|
28
|
+
private_constant :CODEBLOCK_MARKER
|
29
|
+
|
30
|
+
attr_reader :lines
|
31
|
+
attr_accessor :prev, :current
|
32
|
+
|
33
|
+
def init_position
|
34
|
+
self.prev = false
|
35
|
+
self.current = lines.first.include?(CODEBLOCK_MARKER)
|
36
|
+
end
|
37
|
+
|
38
|
+
def update_position(current)
|
39
|
+
self.current = current
|
40
|
+
self.prev = current ? !prev : prev
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "element"
|
4
|
+
require_relative "markdown_traverser"
|
5
|
+
|
6
|
+
module HTML
|
7
|
+
class Pipeline
|
8
|
+
module RubyMarkup
|
9
|
+
class Transformer
|
10
|
+
def self.run(content)
|
11
|
+
new(content).run
|
12
|
+
end
|
13
|
+
|
14
|
+
def initialize(content)
|
15
|
+
@content = content
|
16
|
+
end
|
17
|
+
|
18
|
+
def run
|
19
|
+
transform_ruby_markups!
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
# Matching [漢字(かんじ)] or [漢字(かんじ)](url)
|
25
|
+
RubyTagPattern = %r(
|
26
|
+
(?<!!)
|
27
|
+
\[
|
28
|
+
(?<word>[^\[\(\)]+(?=\())(?<!\s)
|
29
|
+
\((?<reading>[^\[\]]+(?=\)))\)
|
30
|
+
\]
|
31
|
+
(\((?<uri>[^\(\)]+(?=\)))\))*
|
32
|
+
)x
|
33
|
+
RubyMarkupInsideCodePattern = /`.*#{RubyTagPattern}.*`/
|
34
|
+
|
35
|
+
attr_reader :content
|
36
|
+
|
37
|
+
def traverser
|
38
|
+
@traverser ||= MarkdownTraverser.new(content)
|
39
|
+
end
|
40
|
+
|
41
|
+
def transform_ruby_markups!
|
42
|
+
traverser.each do |line|
|
43
|
+
next if traverser.in_codeblock?
|
44
|
+
next if line.match?(RubyMarkupInsideCodePattern)
|
45
|
+
|
46
|
+
matches = line.scan(RubyTagPattern)
|
47
|
+
|
48
|
+
if !matches.empty?
|
49
|
+
matches.each do |word, reading, uri|
|
50
|
+
ruby_element = Element.new(word, reading, uri)
|
51
|
+
line.sub!(ruby_element.original, ruby_element.to_html)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end.join
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html-pipeline-ruby_markup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Juanito Fatas
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: html-pipeline
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
description: A HTML::Pipeline filter to write ruby markup elements.
|
28
|
+
email:
|
29
|
+
- katehuang0320@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- CHANGELOG.md
|
37
|
+
- CONTRIBUTING.md
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.md
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- html-pipeline-ruby_markup.gemspec
|
43
|
+
- lib/html/pipeline/ruby_markup.rb
|
44
|
+
- lib/html/pipeline/ruby_markup/element.rb
|
45
|
+
- lib/html/pipeline/ruby_markup/markdown_traverser.rb
|
46
|
+
- lib/html/pipeline/ruby_markup/transformer.rb
|
47
|
+
- lib/html/pipeline/ruby_markup/version.rb
|
48
|
+
homepage: https://github.com/juanitofatas/html-pipeline-ruby_markup
|
49
|
+
licenses:
|
50
|
+
- MIT
|
51
|
+
metadata: {}
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
requirements: []
|
67
|
+
rubygems_version: 3.1.2
|
68
|
+
signing_key:
|
69
|
+
specification_version: 4
|
70
|
+
summary: A HTML::Pipeline filter to write ruby markup elements.
|
71
|
+
test_files: []
|