jekyll_pilcrow_converter 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +7 -0
- data/README.md +28 -0
- data/lib/jekyll_pilcrow_converter.rb +87 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 15b183df1086148e0bbc61df7d2025c63a5815fc
|
4
|
+
data.tar.gz: f0f9c75cb00993fe717d34a146459a119d02d861
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e96830cdddef84d8277e4e6bbb6d64477dfc2c772b97d3dd8155bc1f86dd7dee456f22a4b89ee7f338538c722abd8ff3b60abc43375e9c8418b0a9cb4b2e7dca
|
7
|
+
data.tar.gz: '09054819b65bfb6a4206ae4a12ca7a7891fae8e08323a48040b2919ead7cb2f019abb8dfd9e23f545cd39546a4f14644fda5ffebd6fbf8bda5991394c1e21b57'
|
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright 2017 Jonathan Hooper
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
This is a converter for Jekyll sites that adds a pilcrow anchor link to the beginning of the paragraphs.
|
2
|
+
|
3
|
+
With this converter, this markdown
|
4
|
+
|
5
|
+
```markdown
|
6
|
+
|
7
|
+
This is a paragraph!
|
8
|
+
|
9
|
+
```
|
10
|
+
|
11
|
+
...generates this HTML...
|
12
|
+
|
13
|
+
```html
|
14
|
+
<p id='123abc'>
|
15
|
+
<a href='#123abc' class='pilcrow'>¶ </a>
|
16
|
+
This is a paragraph!
|
17
|
+
</p>
|
18
|
+
```
|
19
|
+
|
20
|
+
To add this to your Jekyll, just add this to your Gemfile and you're set:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
group :jekyll_plugins do
|
24
|
+
gem 'jekyll_pilcrow_converter'
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
You can build the example site in this repo to see the plugin in action.
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'base64'
|
2
|
+
require 'digest'
|
3
|
+
|
4
|
+
module Jekyll
|
5
|
+
class PilcrowConverter < Converter
|
6
|
+
safe true
|
7
|
+
priority :low
|
8
|
+
|
9
|
+
def matches(ext)
|
10
|
+
ext == '.md'
|
11
|
+
end
|
12
|
+
|
13
|
+
def output_ext(_ext)
|
14
|
+
'.html'
|
15
|
+
end
|
16
|
+
|
17
|
+
def convert(content)
|
18
|
+
document = Kramdown::Document.new(content)
|
19
|
+
document.root = add_pilcrows(document.root)
|
20
|
+
document.to_html
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def add_pilcrows(element)
|
26
|
+
element.children = element.children.inject([]) do |children, child_element|
|
27
|
+
if paragraph_element?(child_element)
|
28
|
+
paragraph = add_pilcrow_to_paragraph(child_element)
|
29
|
+
children + [paragraph]
|
30
|
+
elsif !child_element.children.empty?
|
31
|
+
children + [add_pilcrows(child_element)]
|
32
|
+
else
|
33
|
+
children + [child_element]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
element
|
37
|
+
end
|
38
|
+
|
39
|
+
def paragraph_element?(element)
|
40
|
+
return false if element.children.empty?
|
41
|
+
return false unless element.type == :html_element && element.value == 'p'
|
42
|
+
|
43
|
+
first_child = element.children.first
|
44
|
+
|
45
|
+
return true if first_child.type == :text
|
46
|
+
return true if first_child.type == :html_element && first_child.value == 'a'
|
47
|
+
false
|
48
|
+
end
|
49
|
+
|
50
|
+
def add_pilcrow_to_paragraph(paragraph)
|
51
|
+
id = generate_paragraph_id(paragraph)
|
52
|
+
pilcrow = build_pilcrow_element(id)
|
53
|
+
paragraph.attr[:id] = id
|
54
|
+
paragraph.children = [pilcrow] + paragraph.children
|
55
|
+
paragraph
|
56
|
+
end
|
57
|
+
|
58
|
+
def build_pilcrow_element(id)
|
59
|
+
link = Kramdown::Element.new(
|
60
|
+
:html_element,
|
61
|
+
'a',
|
62
|
+
href: "##{id}",
|
63
|
+
class: 'pilcrow'
|
64
|
+
)
|
65
|
+
text = Kramdown::Element.new(:text, '¶ ')
|
66
|
+
link.children = [text]
|
67
|
+
link
|
68
|
+
end
|
69
|
+
|
70
|
+
def generate_paragraph_id(paragraph)
|
71
|
+
text = get_element_text(paragraph)
|
72
|
+
Base64.urlsafe_encode64(Digest::MD5.new.digest(text))
|
73
|
+
end
|
74
|
+
|
75
|
+
def get_element_text(element)
|
76
|
+
element.children.map do |child_element|
|
77
|
+
if child_element.type == :text
|
78
|
+
child_element.value
|
79
|
+
elsif !child_element.children.empty?
|
80
|
+
get_element_text(child_element)
|
81
|
+
else
|
82
|
+
''
|
83
|
+
end
|
84
|
+
end.join('')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll_pilcrow_converter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jonathan Hooper
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jekyll
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: kramdown
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: |2
|
42
|
+
This gem is a Jekyll plugin that adds an anchor link to the
|
43
|
+
beginning of paragraphs in a rendered site.
|
44
|
+
email: jon@jonathanhooper.net
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- LICENSE
|
50
|
+
- README.md
|
51
|
+
- lib/jekyll_pilcrow_converter.rb
|
52
|
+
homepage: https://github.com/jmhooper/jekyll_pilcrow_converter
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata:
|
56
|
+
source_code_uri: https://github.com/jmhooper/jekyll_pilcrow_converter
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.6.13
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: Add pilcrow links to paragraphs
|
77
|
+
test_files: []
|