protos-markdown 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/SECURITY.md +6 -0
- data/lib/protos/markdown/ast.rb +37 -0
- data/lib/protos/markdown/table.rb +55 -0
- data/lib/protos/markdown.rb +157 -0
- data/lib/protos-markdown.rb +3 -0
- data/protos-markdown.gemspec +37 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2d4499ceae5e6f824d04bd43384a5b59ff7b45c0b452064147e92c80e399b0a8
|
4
|
+
data.tar.gz: 9682d3c772d9693dc3fbd4cb0d1906c9c8e70333f584139a680cb214daf15d84
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a671f9021664a20db7495331fa99e711d9adbb02c9cea2c833b1806517c7f56a3a4662c5b16d2ef2d04e8ba4263f2cc1815f3cb36881ec37a3f5e00cba9822e
|
7
|
+
data.tar.gz: ac9acd00942037b64467b160adc04336672d8f428edb1848fad1870132972497ec8de967b8fef180bafb476ce4a6ef7a07c667cf58944e78e8c49de1c506ebd9
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Nolan J Tait
|
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,83 @@
|
|
1
|
+
# Protos Markdown
|
2
|
+
|
3
|
+
Markdown component built with [Protos](https://github.com/inhouse-work/protos).
|
4
|
+
|
5
|
+
This is a fork of [phlex-markdown](https://github.com/phlex-ruby/phlex-markdown).
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add protos-markdown
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by
|
14
|
+
executing:
|
15
|
+
|
16
|
+
$ gem install protos-markdown
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
This library lets you define your own component and override elements, including
|
21
|
+
using the standard protos conventions:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Markdown < Protos::Markdown
|
25
|
+
def h1 = super(class: css[:title])
|
26
|
+
def ul = super(class: "ml-4 pt-2")
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def theme
|
31
|
+
{
|
32
|
+
title: "font-bold text-xl"
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Rendering the component outputs our custom css:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
content = <<~MD
|
42
|
+
# Hello World
|
43
|
+
|
44
|
+
- A
|
45
|
+
- B
|
46
|
+
- C
|
47
|
+
MD
|
48
|
+
|
49
|
+
output = Markdown.new(content).call
|
50
|
+
```
|
51
|
+
|
52
|
+
Which outputs the following html:
|
53
|
+
|
54
|
+
```html
|
55
|
+
<h1 class="font-bold text-xl">Hello World</h1>
|
56
|
+
<ul class="ml-4 pt-2">
|
57
|
+
<li>A</li>
|
58
|
+
<li>B</li>
|
59
|
+
<li>C</li>
|
60
|
+
</ul>
|
61
|
+
```
|
62
|
+
|
63
|
+
## Development
|
64
|
+
|
65
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
66
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
67
|
+
prompt that will allow you to experiment.
|
68
|
+
|
69
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
70
|
+
release a new version, update the version number in `version.rb`, and then run
|
71
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
72
|
+
git commits and the created tag, and push the `.gem` file to
|
73
|
+
[rubygems.org](https://rubygems.org).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at
|
78
|
+
https://github.com/inhouse-work/protos-markdown.
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the
|
83
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
data/SECURITY.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Protos
|
4
|
+
class Markdown < ::Protos::Component
|
5
|
+
class AST
|
6
|
+
class Node < SimpleDelegator
|
7
|
+
def accept(visitor)
|
8
|
+
visitor.send(:"visit_#{type}", self)
|
9
|
+
end
|
10
|
+
|
11
|
+
def each(&block)
|
12
|
+
return enum_for(:each) unless block
|
13
|
+
|
14
|
+
super do |child|
|
15
|
+
yield Node.new(child)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.parse(content)
|
21
|
+
Markly
|
22
|
+
.parse(content, flags: Markly::GITHUB_PRE_LANG, extensions: [:table])
|
23
|
+
.then { |node| new(Node.new(node)) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def initialize(root)
|
27
|
+
@root = root
|
28
|
+
end
|
29
|
+
|
30
|
+
def accept(visitor)
|
31
|
+
@root.each do |node|
|
32
|
+
Node.new(node).accept(visitor)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Protos
|
2
|
+
class Markdown
|
3
|
+
class Table < Protos::Table
|
4
|
+
option :inside_header, default: -> { false }, reader: false
|
5
|
+
|
6
|
+
def visit_table(node)
|
7
|
+
visit_children(node)
|
8
|
+
end
|
9
|
+
|
10
|
+
def visit_table_header(node)
|
11
|
+
@inside_header = true
|
12
|
+
|
13
|
+
header do
|
14
|
+
visit_children(node)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def visit_table_cell(node)
|
19
|
+
if @inside_header
|
20
|
+
head { visit_children(node) }
|
21
|
+
else
|
22
|
+
cell { visit_children(node) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def visit_text(node)
|
27
|
+
plain(node.string_content)
|
28
|
+
end
|
29
|
+
|
30
|
+
def visit_table_row(node)
|
31
|
+
@inside_header = false
|
32
|
+
|
33
|
+
row do
|
34
|
+
visit_children(node)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def visit_code(node)
|
39
|
+
code { node.string_content }
|
40
|
+
end
|
41
|
+
|
42
|
+
def visit_strong(node)
|
43
|
+
strong { visit_children(node) }
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def visit_children(node)
|
49
|
+
node.each do |child|
|
50
|
+
child.accept(self)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "protos"
|
4
|
+
require "markly"
|
5
|
+
require "rouge"
|
6
|
+
require "delegate"
|
7
|
+
|
8
|
+
require_relative "markdown/ast"
|
9
|
+
require_relative "markdown/table"
|
10
|
+
|
11
|
+
module Protos
|
12
|
+
class Markdown < ::Protos::Component # rubocop:disable Metrics/ClassLength
|
13
|
+
param :content, reader: false
|
14
|
+
option :sanitize, default: -> { true }, reader: false
|
15
|
+
|
16
|
+
def view_template
|
17
|
+
return unless root
|
18
|
+
|
19
|
+
root.accept(self)
|
20
|
+
end
|
21
|
+
|
22
|
+
def visit_document(_node)
|
23
|
+
# Do nothing
|
24
|
+
end
|
25
|
+
|
26
|
+
def visit_table(node)
|
27
|
+
render Markdown::Table.new do |table|
|
28
|
+
node.accept(table)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def visit_softbreak(_node)
|
33
|
+
whitespace
|
34
|
+
end
|
35
|
+
|
36
|
+
def visit_text(node)
|
37
|
+
plain(node.string_content)
|
38
|
+
end
|
39
|
+
|
40
|
+
def visit_header(node)
|
41
|
+
case node.header_level
|
42
|
+
in 1 then h1 { visit_children(node) }
|
43
|
+
in 2 then h2 { visit_children(node) }
|
44
|
+
in 3 then h3 { visit_children(node) }
|
45
|
+
in 4 then h4 { visit_children(node) }
|
46
|
+
in 5 then h5 { visit_children(node) }
|
47
|
+
in 6 then h6 { visit_children(node) }
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def visit_paragraph(node)
|
52
|
+
grandparent = node.parent&.parent
|
53
|
+
|
54
|
+
if grandparent&.type == :list && grandparent&.list_tight
|
55
|
+
visit_children(node)
|
56
|
+
else
|
57
|
+
p { visit_children(node) }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def visit_link(node)
|
62
|
+
a(href: node.url, title: node.title) { visit_children(node) }
|
63
|
+
end
|
64
|
+
|
65
|
+
def visit_image(node)
|
66
|
+
img(
|
67
|
+
src: node.url,
|
68
|
+
alt: node.each.first.string_content,
|
69
|
+
title: node.title
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
def visit_emph(node)
|
74
|
+
em { visit_children(node) }
|
75
|
+
end
|
76
|
+
|
77
|
+
def visit_strong(node)
|
78
|
+
strong { visit_children(node) }
|
79
|
+
end
|
80
|
+
|
81
|
+
def visit_list(node)
|
82
|
+
case node.list_type
|
83
|
+
when :ordered_list then ol { visit_children(node) }
|
84
|
+
when :bullet_list then ul { visit_children(node) }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def visit_list_item(node)
|
89
|
+
li { visit_children(node) }
|
90
|
+
end
|
91
|
+
|
92
|
+
def visit_code(node)
|
93
|
+
inline_code do |**attributes|
|
94
|
+
code(**attributes) { plain(node.string_content) }
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def visit_code_block(node)
|
99
|
+
code_block(node.string_content, node.fence_info) do |**attributes|
|
100
|
+
pre(**attributes) do
|
101
|
+
code(class: "highlight language-#{node.fence_info}") do
|
102
|
+
unsafe_raw lex(node.string_content, node.fence_info)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def visit_hrule(_node)
|
109
|
+
hr
|
110
|
+
end
|
111
|
+
|
112
|
+
def visit_blockquote(node)
|
113
|
+
blockquote { visit_children(node) }
|
114
|
+
end
|
115
|
+
|
116
|
+
def visit_html(node)
|
117
|
+
return if @sanitize
|
118
|
+
|
119
|
+
unsafe_raw(node.string_content)
|
120
|
+
end
|
121
|
+
|
122
|
+
def visit_inline_html(node)
|
123
|
+
return if @sanitize
|
124
|
+
|
125
|
+
unsafe_raw(node.string_content)
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def root
|
131
|
+
AST.parse(@content)
|
132
|
+
end
|
133
|
+
|
134
|
+
def formatter
|
135
|
+
@formatter ||= Rouge::Formatters::HTML.new
|
136
|
+
end
|
137
|
+
|
138
|
+
def lex(source, language)
|
139
|
+
lexer = Rouge::Lexer.find(language)
|
140
|
+
return source if lexer.nil?
|
141
|
+
|
142
|
+
formatter.format(lexer.lex(source))
|
143
|
+
end
|
144
|
+
|
145
|
+
def inline_code(**attributes)
|
146
|
+
yield(**attributes)
|
147
|
+
end
|
148
|
+
|
149
|
+
def code_block(code, language, **attributes) # rubocop:disable Lint/UnusedMethodArgument, Metrics/ParameterLists
|
150
|
+
yield(**attributes)
|
151
|
+
end
|
152
|
+
|
153
|
+
def visit_children(node)
|
154
|
+
node.each { |child| child.accept(self) }
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "protos-markdown"
|
5
|
+
spec.version = "0.1.0"
|
6
|
+
spec.authors = ["Nolan Tait"]
|
7
|
+
spec.email = ["nolanjtait@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "A markdown renderer with Phlex and Protos"
|
10
|
+
spec.description = "A markdown renderer with Phlex and Protos"
|
11
|
+
spec.homepage = "https://github.com/inhouse-work/protos-markdown"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = ">= 3.1"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
18
|
+
spec.metadata["funding_uri"] = spec.homepage
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
22
|
+
# into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(File.expand_path(f) == __FILE__) ||
|
26
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_dependency "markly", "~> 0.7"
|
34
|
+
spec.add_dependency "protos", ">= 0.4"
|
35
|
+
|
36
|
+
spec.metadata["rubygems_mfa_required"] = "true"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: protos-markdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nolan Tait
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: markly
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.7'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: protos
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.4'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.4'
|
41
|
+
description: A markdown renderer with Phlex and Protos
|
42
|
+
email:
|
43
|
+
- nolanjtait@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".rspec"
|
49
|
+
- ".rubocop.yml"
|
50
|
+
- CHANGELOG.md
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- SECURITY.md
|
54
|
+
- lib/protos-markdown.rb
|
55
|
+
- lib/protos/markdown.rb
|
56
|
+
- lib/protos/markdown/ast.rb
|
57
|
+
- lib/protos/markdown/table.rb
|
58
|
+
- protos-markdown.gemspec
|
59
|
+
homepage: https://github.com/inhouse-work/protos-markdown
|
60
|
+
licenses:
|
61
|
+
- MIT
|
62
|
+
metadata:
|
63
|
+
homepage_uri: https://github.com/inhouse-work/protos-markdown
|
64
|
+
source_code_uri: https://github.com/inhouse-work/protos-markdown
|
65
|
+
changelog_uri: https://github.com/inhouse-work/protos-markdown
|
66
|
+
funding_uri: https://github.com/inhouse-work/protos-markdown
|
67
|
+
rubygems_mfa_required: 'true'
|
68
|
+
post_install_message:
|
69
|
+
rdoc_options: []
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '3.1'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubygems_version: 3.5.17
|
84
|
+
signing_key:
|
85
|
+
specification_version: 4
|
86
|
+
summary: A markdown renderer with Phlex and Protos
|
87
|
+
test_files: []
|