markdown_formatter 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +1 -0
  4. data/.gitignore +12 -0
  5. data/.rspec +3 -0
  6. data/.travis.yml +5 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +46 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/exe/mdfmt +27 -0
  14. data/lib/markdown_formatter.rb +57 -0
  15. data/lib/markdown_formatter/ast.rb +47 -0
  16. data/lib/markdown_formatter/cli.rb +13 -0
  17. data/lib/markdown_formatter/nodes/base.rb +11 -0
  18. data/lib/markdown_formatter/nodes/blank.rb +9 -0
  19. data/lib/markdown_formatter/nodes/block_quote.rb +22 -0
  20. data/lib/markdown_formatter/nodes/code_block.rb +9 -0
  21. data/lib/markdown_formatter/nodes/code_span.rb +9 -0
  22. data/lib/markdown_formatter/nodes/container_block.rb +23 -0
  23. data/lib/markdown_formatter/nodes/emphasis.rb +10 -0
  24. data/lib/markdown_formatter/nodes/entity.rb +9 -0
  25. data/lib/markdown_formatter/nodes/footnote.rb +9 -0
  26. data/lib/markdown_formatter/nodes/header.rb +9 -0
  27. data/lib/markdown_formatter/nodes/html_element.rb +9 -0
  28. data/lib/markdown_formatter/nodes/image.rb +9 -0
  29. data/lib/markdown_formatter/nodes/inline.rb +29 -0
  30. data/lib/markdown_formatter/nodes/leaf_block.rb +27 -0
  31. data/lib/markdown_formatter/nodes/link.rb +9 -0
  32. data/lib/markdown_formatter/nodes/list.rb +23 -0
  33. data/lib/markdown_formatter/nodes/list_item.rb +38 -0
  34. data/lib/markdown_formatter/nodes/paragraph.rb +39 -0
  35. data/lib/markdown_formatter/nodes/smart_quote.rb +14 -0
  36. data/lib/markdown_formatter/nodes/strong.rb +10 -0
  37. data/lib/markdown_formatter/nodes/thematic_break.rb +9 -0
  38. data/lib/markdown_formatter/nodes/typographic_sym.rb +9 -0
  39. data/lib/markdown_formatter/version.rb +3 -0
  40. data/lib/monkey_patches/kramdown/parser/gfm.rb +21 -0
  41. data/lib/monkey_patches/kramdown/parser/kramdown/autolink.rb +16 -0
  42. data/lib/monkey_patches/kramdown/parser/kramdown/blockquote.rb +22 -0
  43. data/lib/monkey_patches/kramdown/parser/kramdown/code_block.rb +34 -0
  44. data/lib/monkey_patches/kramdown/parser/kramdown/code_span.rb +31 -0
  45. data/lib/monkey_patches/kramdown/parser/kramdown/emphasis.rb +48 -0
  46. data/lib/monkey_patches/kramdown/parser/kramdown/escaped_chars.rb +12 -0
  47. data/lib/monkey_patches/kramdown/parser/kramdown/footnote.rb +27 -0
  48. data/lib/monkey_patches/kramdown/parser/kramdown/header.rb +38 -0
  49. data/lib/monkey_patches/kramdown/parser/kramdown/horizontal_rule.rb +14 -0
  50. data/lib/monkey_patches/kramdown/parser/kramdown/html.rb +60 -0
  51. data/lib/monkey_patches/kramdown/parser/kramdown/link.rb +116 -0
  52. data/lib/monkey_patches/kramdown/parser/kramdown/list.rb +115 -0
  53. data/lib/monkey_patches/kramdown/parser/kramdown/paragraph.rb +26 -0
  54. data/lib/monkey_patches/kramdown/parser/kramdown/typographic_symbol.rb +26 -0
  55. data/lib/monkey_patches/kramdown/utils/entities.rb +1911 -0
  56. data/markdown_formatter.gemspec +29 -0
  57. metadata +192 -0
  58. metadata.gz.sig +0 -0
@@ -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
Binary file
@@ -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¼�;��(�#��
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in markdown_formatter.gemspec
4
+ gemspec
@@ -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.
@@ -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 &copy; 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/)
@@ -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
@@ -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__)
@@ -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,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,13 @@
1
+ module MarkdownFormatter
2
+ class CLI
3
+ def self.run(filepath)
4
+ result = MarkdownFormatter.format(File.read(filepath))
5
+
6
+ if MarkdownFormatter::OPTIONS[:overwrite]
7
+ File.write(filepath, result)
8
+ else
9
+ puts result
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class Base
4
+ attr_reader :node
5
+
6
+ def initialize(node)
7
+ @node = node
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class Blank < Base
4
+ def to_s
5
+ node[:value]
6
+ end
7
+ end
8
+ end
9
+ 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,9 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class CodeBlock < Base
4
+ def to_s
5
+ node.dig(:options, :raw_text).chomp.strip_heredoc
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class CodeSpan < Base
4
+ def to_s
5
+ node.dig(:options, :raw_text).gsub(/\R/, " ")
6
+ end
7
+ end
8
+ end
9
+ 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
@@ -0,0 +1,10 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class Emphasis < Base
4
+ def to_s
5
+ delim = node.dig(:options, :delim)
6
+ "%s%s%s" % [delim, Paragraph.new(node).to_s, delim]
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class Entity < Base
4
+ def to_s
5
+ node.dig(:options, :original)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class Footnote < Base
4
+ def to_s
5
+ node.dig(:options, :raw_text)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class Header < Base
4
+ def to_s
5
+ node.dig(:options, :original_text)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module MarkdownFormatter
2
+ module ASTNode
3
+ class HTMLElement < Base
4
+ def to_s
5
+ "%s%s</%s>" % [node.dig(:options, :opening_tag), Paragraph.new(node).to_s, node[:value]]
6
+ end
7
+ end
8
+ end
9
+ end