markdown_formatter 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +1 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/mdfmt +27 -0
- data/lib/markdown_formatter.rb +57 -0
- data/lib/markdown_formatter/ast.rb +47 -0
- data/lib/markdown_formatter/cli.rb +13 -0
- data/lib/markdown_formatter/nodes/base.rb +11 -0
- data/lib/markdown_formatter/nodes/blank.rb +9 -0
- data/lib/markdown_formatter/nodes/block_quote.rb +22 -0
- data/lib/markdown_formatter/nodes/code_block.rb +9 -0
- data/lib/markdown_formatter/nodes/code_span.rb +9 -0
- data/lib/markdown_formatter/nodes/container_block.rb +23 -0
- data/lib/markdown_formatter/nodes/emphasis.rb +10 -0
- data/lib/markdown_formatter/nodes/entity.rb +9 -0
- data/lib/markdown_formatter/nodes/footnote.rb +9 -0
- data/lib/markdown_formatter/nodes/header.rb +9 -0
- data/lib/markdown_formatter/nodes/html_element.rb +9 -0
- data/lib/markdown_formatter/nodes/image.rb +9 -0
- data/lib/markdown_formatter/nodes/inline.rb +29 -0
- data/lib/markdown_formatter/nodes/leaf_block.rb +27 -0
- data/lib/markdown_formatter/nodes/link.rb +9 -0
- data/lib/markdown_formatter/nodes/list.rb +23 -0
- data/lib/markdown_formatter/nodes/list_item.rb +38 -0
- data/lib/markdown_formatter/nodes/paragraph.rb +39 -0
- data/lib/markdown_formatter/nodes/smart_quote.rb +14 -0
- data/lib/markdown_formatter/nodes/strong.rb +10 -0
- data/lib/markdown_formatter/nodes/thematic_break.rb +9 -0
- data/lib/markdown_formatter/nodes/typographic_sym.rb +9 -0
- data/lib/markdown_formatter/version.rb +3 -0
- data/lib/monkey_patches/kramdown/parser/gfm.rb +21 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/autolink.rb +16 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/blockquote.rb +22 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/code_block.rb +34 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/code_span.rb +31 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/emphasis.rb +48 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/escaped_chars.rb +12 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/footnote.rb +27 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/header.rb +38 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/horizontal_rule.rb +14 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/html.rb +60 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/link.rb +116 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/list.rb +115 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/paragraph.rb +26 -0
- data/lib/monkey_patches/kramdown/parser/kramdown/typographic_symbol.rb +26 -0
- data/lib/monkey_patches/kramdown/utils/entities.rb +1911 -0
- data/markdown_formatter.gemspec +29 -0
- metadata +192 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: cdd3b74679d7aa16c5765abe754fb06c4bbee2fd
|
4
|
+
data.tar.gz: 3703602b1af4af696464005232c8161c71e11371
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1e45c53ebb1e521c5420d5f602d44ebdbdb3f3eed33a1b5fc5d78d603cecd35c14b41970f721fed9da194c23bab69ea6bd9487f42b06b0a08bbc6544ade47337
|
7
|
+
data.tar.gz: f272dd9370d2914ba7a89890610daeb5b0b3ae816c624cdcc7de0041abc3d41491530fe2e0eecbe3feb76438a2df8c642c93189cc55f38d22821c523eb27abb2
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data.tar.gz.sig
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
�X�j�D#"����F�I�o�r.�C���+��&y�e0��VI�-���3���(��q�#ʖ�����<H f^����Ƭ��q'�u���}����"~�h�����Aɷ����g�غ�U¼�;��(�#��
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 YassLab
|
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
|
+
# MarkdownFormatter
|
2
|
+
|
3
|
+
Markdown formatter that removes unnecessary newlines, written in Ruby.
|
4
|
+
This polishes segments for making [Translation Memory](https://en.wikipedia.org/wiki/Translation_memory) more maintainable.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'markdown_formatter'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install markdown_formatter
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
```
|
25
|
+
Usage: mdfmt [options] filepath
|
26
|
+
-w, --overwrite overwrite file instead of stdout
|
27
|
+
-v, --version display the version.
|
28
|
+
```
|
29
|
+
|
30
|
+
## Development
|
31
|
+
|
32
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
33
|
+
|
34
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
35
|
+
|
36
|
+
## Contributing
|
37
|
+
|
38
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yasslab/markdown_formatter. 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.
|
39
|
+
|
40
|
+
## License
|
41
|
+
|
42
|
+
Copyright © 2017 [YassLab](https://yasslab.jp)
|
43
|
+
|
44
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
45
|
+
|
46
|
+
[![YassLab Logo](https://yasslab.jp/img/logo_800x200.png)](https://yasslab.jp/)
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "markdown_formatter"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/exe/mdfmt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "markdown_formatter"
|
4
|
+
require "optparse"
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
|
8
|
+
opt_parse = OptionParser.new do |opt|
|
9
|
+
opt.banner = 'Usage: mdfmt [options] markdownfile'
|
10
|
+
opt.on('-w', '--overwrite', 'overwrite file instead of stdout') { options[:overwrite] = true }
|
11
|
+
opt.on('-v', '--version', 'display the version.') { options[:version] = true }
|
12
|
+
opt.parse!(ARGV)
|
13
|
+
end
|
14
|
+
|
15
|
+
filepath = ARGV.first
|
16
|
+
|
17
|
+
MarkdownFormatter::OPTIONS.merge!(options)
|
18
|
+
|
19
|
+
case
|
20
|
+
when options[:version]
|
21
|
+
puts "MarkdownFormatter version: #{MarkdownFormatter::VERSION}"
|
22
|
+
when filepath.nil?
|
23
|
+
puts opt_parse
|
24
|
+
else
|
25
|
+
MarkdownFormatter::CLI.run(filepath.chomp)
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "kramdown"
|
2
|
+
require "active_support"
|
3
|
+
require "active_support/core_ext"
|
4
|
+
require "pp"
|
5
|
+
|
6
|
+
require "markdown_formatter/version"
|
7
|
+
require "markdown_formatter/ast"
|
8
|
+
require "markdown_formatter/cli"
|
9
|
+
|
10
|
+
require "markdown_formatter/nodes/base"
|
11
|
+
require "markdown_formatter/nodes/link"
|
12
|
+
require "markdown_formatter/nodes/leaf_block"
|
13
|
+
require "markdown_formatter/nodes/blank"
|
14
|
+
require "markdown_formatter/nodes/leaf_block"
|
15
|
+
require "markdown_formatter/nodes/container_block"
|
16
|
+
require "markdown_formatter/nodes/inline"
|
17
|
+
require "markdown_formatter/nodes/code_span"
|
18
|
+
require "markdown_formatter/nodes/emphasis"
|
19
|
+
require "markdown_formatter/nodes/entity"
|
20
|
+
require "markdown_formatter/nodes/image"
|
21
|
+
require "markdown_formatter/nodes/header"
|
22
|
+
require "markdown_formatter/nodes/thematic_break"
|
23
|
+
require "markdown_formatter/nodes/paragraph"
|
24
|
+
require "markdown_formatter/nodes/smart_quote"
|
25
|
+
require "markdown_formatter/nodes/strong"
|
26
|
+
require "markdown_formatter/nodes/html_element"
|
27
|
+
require "markdown_formatter/nodes/code_block"
|
28
|
+
require "markdown_formatter/nodes/footnote"
|
29
|
+
require "markdown_formatter/nodes/typographic_sym"
|
30
|
+
require "markdown_formatter/nodes/list_item"
|
31
|
+
require "markdown_formatter/nodes/list"
|
32
|
+
require "markdown_formatter/nodes/block_quote"
|
33
|
+
|
34
|
+
# Monkey Patch
|
35
|
+
require "monkey_patches/kramdown/parser/kramdown/autolink"
|
36
|
+
require "monkey_patches/kramdown/parser/kramdown/blockquote"
|
37
|
+
require "monkey_patches/kramdown/parser/kramdown/code_span"
|
38
|
+
require "monkey_patches/kramdown/parser/kramdown/emphasis"
|
39
|
+
require "monkey_patches/kramdown/parser/kramdown/escaped_chars"
|
40
|
+
require "monkey_patches/kramdown/parser/kramdown/footnote"
|
41
|
+
require "monkey_patches/kramdown/parser/kramdown/link"
|
42
|
+
require "monkey_patches/kramdown/parser/kramdown/html"
|
43
|
+
require "monkey_patches/kramdown/parser/kramdown/typographic_symbol"
|
44
|
+
require "monkey_patches/kramdown/parser/kramdown/list"
|
45
|
+
require "monkey_patches/kramdown/parser/kramdown/header"
|
46
|
+
require "monkey_patches/kramdown/parser/kramdown/paragraph"
|
47
|
+
require "monkey_patches/kramdown/parser/kramdown/code_block"
|
48
|
+
require "monkey_patches/kramdown/utils/entities"
|
49
|
+
require "monkey_patches/kramdown/parser/gfm"
|
50
|
+
|
51
|
+
module MarkdownFormatter
|
52
|
+
OPTIONS = {}
|
53
|
+
|
54
|
+
def self.format(source)
|
55
|
+
AST.new(source).to_s
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module MarkdownFormatter
|
2
|
+
class AST
|
3
|
+
def initialize(source)
|
4
|
+
@source = source.dup
|
5
|
+
doc = Kramdown::Document.new(source.dup, input: "GFM")
|
6
|
+
@ast, @warnings = doc.to_hash_ast
|
7
|
+
end
|
8
|
+
|
9
|
+
def traverse(parent = [@ast])
|
10
|
+
parent.each do |node|
|
11
|
+
case node[:type]
|
12
|
+
when :root
|
13
|
+
traverse(node[:children])
|
14
|
+
when :blank
|
15
|
+
# skip event
|
16
|
+
when :p
|
17
|
+
str = ASTNode::Paragraph.new(node).to_s
|
18
|
+
|
19
|
+
unless @source.sub!(node.dig(:options, :raw_text).chomp) { |m| str.gsub(/\R(?!\z)/, " ").chomp }
|
20
|
+
raise "Parse Failed!!"
|
21
|
+
end
|
22
|
+
when :ul, :ol, :blockquote
|
23
|
+
str = ASTNode::ContainerBlock.new(node).to_s
|
24
|
+
unless @source.sub!(node.dig(:options, :raw_text), str)
|
25
|
+
raise "Parse Failed!!"
|
26
|
+
end
|
27
|
+
when :header
|
28
|
+
# skip event
|
29
|
+
when :hr
|
30
|
+
# skip event
|
31
|
+
when :codeblock
|
32
|
+
# skip event
|
33
|
+
when :table
|
34
|
+
# TODO: implements Table node.
|
35
|
+
else
|
36
|
+
pp node
|
37
|
+
raise "Unexpected type #{node[:type]}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
traverse
|
44
|
+
@source
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module MarkdownFormatter
|
2
|
+
module ASTNode
|
3
|
+
class BlockQuote < Base
|
4
|
+
def to_s
|
5
|
+
node[:children].map do |c|
|
6
|
+
case c[:type]
|
7
|
+
when :blank
|
8
|
+
">"
|
9
|
+
when :header
|
10
|
+
"> " + Header.new(c).to_s.gsub(/\R/, "\n> ")
|
11
|
+
when :ol, :ul
|
12
|
+
"> " + List.new(c).to_s.gsub(/\R(?!\z)/, "\n> ")
|
13
|
+
when :blockquote
|
14
|
+
"> " + BlockQuote.new(c).to_s
|
15
|
+
else
|
16
|
+
"> " + Paragraph.new(c).to_s.gsub(/\R/, " ")
|
17
|
+
end
|
18
|
+
end.join("\n").chomp + "\n"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MarkdownFormatter
|
2
|
+
module ASTNode
|
3
|
+
class ContainerBlock < Base
|
4
|
+
def to_s
|
5
|
+
str = ""
|
6
|
+
|
7
|
+
case node[:type]
|
8
|
+
when :blockquote
|
9
|
+
str += ASTNode::BlockQuote.new(node).to_s
|
10
|
+
when :ul, :ol
|
11
|
+
str += ASTNode::List.new(node).to_s
|
12
|
+
when :table
|
13
|
+
# TODO: implements Table node.
|
14
|
+
else
|
15
|
+
pp node
|
16
|
+
raise "Unexpected type #{node[:type]}"
|
17
|
+
end
|
18
|
+
|
19
|
+
str
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|