source2md 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +112 -29
  3. data/Rakefile +1 -0
  4. data/doc/0100_top.rb +7 -0
  5. data/doc/0110_code_small.rb +4 -1
  6. data/doc/0120_code_long.rb +2 -1
  7. data/doc/0130_hidden.rb +17 -0
  8. data/doc/0140_code_include.rb +26 -0
  9. data/doc/0150_raw_include.rb +16 -0
  10. data/doc/{0130_title.rb → 0160_title.rb} +8 -5
  11. data/doc/{0140_md_like_title.rb → 0170_md_like_title.rb} +9 -6
  12. data/doc/{0150_table.rb → 0180_table.rb} +2 -1
  13. data/doc/{0160_method.rb → 0190_method.rb} +2 -1
  14. data/doc/{0170_alert.rb → 0200_alert.rb} +2 -1
  15. data/doc/0210_text.rb +18 -0
  16. data/doc/0220_text_oneline.rb +18 -0
  17. data/doc/0230_parse_include.rb +18 -0
  18. data/examples/cli.rb +1 -0
  19. data/examples/element_text.rb +2 -1
  20. data/examples/scanner.rb +1 -0
  21. data/examples/{element_alert.rb → type_alert.rb} +6 -5
  22. data/examples/type_code_include.rb +21 -0
  23. data/examples/{element_md_header.rb → type_hidden.rb} +2 -1
  24. data/examples/{element_md_title.rb → type_md_title.rb} +2 -1
  25. data/examples/{element_method.rb → type_method.rb} +2 -1
  26. data/examples/{element_partial_code.rb → type_partial_code.rb} +2 -1
  27. data/examples/{element_source_block.rb → type_source_block.rb} +2 -1
  28. data/examples/{element_table.rb → type_table.rb} +1 -0
  29. data/examples/{element_deep_comment.rb → type_text.rb} +16 -10
  30. data/examples/{element_title.rb → type_title.rb} +2 -1
  31. data/examples/{element_warn.rb → type_warn.rb} +2 -1
  32. data/lib/source2md/code_block.rb +12 -6
  33. data/lib/source2md/element.rb +17 -16
  34. data/lib/source2md/{type → formatter}/base.rb +4 -5
  35. data/lib/source2md/{type/element_alert.rb → formatter/type_alert.rb} +4 -4
  36. data/lib/source2md/{type/element_include.rb → formatter/type_code_include.rb} +5 -5
  37. data/lib/source2md/{type/element_else.rb → formatter/type_else.rb} +2 -2
  38. data/lib/source2md/{type/element_reject.rb → formatter/type_hidden.rb} +4 -3
  39. data/lib/source2md/{type/element_md_title.rb → formatter/type_md_title.rb} +2 -2
  40. data/lib/source2md/formatter/type_method.rb +24 -0
  41. data/lib/source2md/formatter/type_parse_include.rb +13 -0
  42. data/lib/source2md/{type/element_partial_code.rb → formatter/type_partial_code.rb} +2 -2
  43. data/lib/source2md/formatter/type_raw_include.rb +13 -0
  44. data/lib/source2md/{type/element_source_block.rb → formatter/type_source_block.rb} +2 -2
  45. data/lib/source2md/{type/element_table.rb → formatter/type_table.rb} +2 -2
  46. data/lib/source2md/formatter/type_text.rb +31 -0
  47. data/lib/source2md/{type/element_title.rb → formatter/type_title.rb} +2 -2
  48. data/lib/source2md/{type/element_warn.rb → formatter/type_warn.rb} +4 -4
  49. data/lib/source2md/generator.rb +3 -3
  50. data/lib/source2md/scanner.rb +2 -2
  51. data/lib/source2md/text_helper.rb +5 -1
  52. data/lib/source2md/version.rb +1 -1
  53. data/spec/code_block_spec.rb +1 -1
  54. data/spec/formatter/sample.yml +1 -0
  55. data/spec/{type/element_include_spec.rb → formatter/type_code_include_spec.rb} +6 -6
  56. data/spec/{type/element_reject_spec.rb → formatter/type_hidden_spec.rb} +3 -2
  57. data/spec/formatter/type_parse_include_spec.rb +16 -0
  58. data/spec/{type/element_md_header_spec.rb → formatter/type_raw_include_spec.rb} +2 -6
  59. data/spec/{type/element_text_spec.rb → formatter/type_text_spec.rb} +14 -2
  60. metadata +67 -62
  61. data/doc/0180_text.rb +0 -15
  62. data/doc/0190_raw.rb +0 -19
  63. data/examples/element_include.rb +0 -20
  64. data/examples/element_reject.rb +0 -17
  65. data/lib/source2md/type/element_deep_comment.rb +0 -19
  66. data/lib/source2md/type/element_md_header.rb +0 -13
  67. data/lib/source2md/type/element_method.rb +0 -24
  68. data/lib/source2md/type/element_uncomment.rb +0 -24
  69. data/spec/type/sample.yml +0 -1
  70. /data/spec/{type/element_alert_spec.rb → formatter/type_alert_spec.rb} +0 -0
  71. /data/spec/{type/element_md_title_spec.rb → formatter/type_md_title_spec.rb} +0 -0
  72. /data/spec/{type/element_method_spec.rb → formatter/type_method_spec.rb} +0 -0
  73. /data/spec/{type/element_partial_code_spec.rb → formatter/type_partial_code_spec.rb} +0 -0
  74. /data/spec/{type/element_source_block_spec.rb → formatter/type_source_block_spec.rb} +0 -0
  75. /data/spec/{type/element_table_spec.rb → formatter/type_table_spec.rb} +0 -0
  76. /data/spec/{type/element_title_spec.rb → formatter/type_title_spec.rb} +0 -0
  77. /data/spec/{type/element_warn_spec.rb → formatter/type_warn_spec.rb} +0 -0
@@ -1,10 +1,11 @@
1
+ #+hidden: true
1
2
  require "./setup"
2
3
 
3
4
  puts Source2MD::Element.new(<<~EOS).to_md
4
5
  #+info: (foo)
5
6
  EOS
6
7
  # >> --------------------------------------------------------------------------------
7
- # >> Source2MD::Type::ElementInfo
8
+ # >> Source2MD::Part::ElementInfo
8
9
  # >>
9
10
  # >> head:
10
11
  # >> {:info=>"(foo)"}
@@ -8,7 +8,8 @@ module Source2MD
8
8
  def initialize(code, options = {})
9
9
  @code = code
10
10
  @options = {
11
- lang: "ruby",
11
+ :lang => "ruby",
12
+ # :single_sharp_replace_to_blank_line => false,
12
13
  }.merge(options)
13
14
  end
14
15
 
@@ -43,13 +44,18 @@ module Source2MD
43
44
  end
44
45
 
45
46
  def normalize(line)
46
- line = single_sharp_replace_to_blank_line(line)
47
- line = comment_mark_justfiy(line)
47
+ # if @options[:single_sharp_replace_to_blank_line]
48
+ # line = single_sharp_replace_to_blank_line(line)
49
+ # end
50
+ if @options[:lang] == "ruby"
51
+ line = comment_mark_justfiy(line)
52
+ end
53
+ line
48
54
  end
49
55
 
50
- def single_sharp_replace_to_blank_line(line)
51
- line.gsub(/\A#\z/, "")
52
- end
56
+ # def single_sharp_replace_to_blank_line(line)
57
+ # line.gsub(/\A#\z/, "")
58
+ # end
53
59
 
54
60
  def comment_mark_justfiy(line)
55
61
  line.gsub(/(.*?)\s*#{MARK}(.*)/) {
@@ -1,21 +1,22 @@
1
1
  module Source2MD
2
2
  class Element
3
- KEY_VALUE = /^#\+(\S+):\s*(.*)\R?/
3
+ KEY_VALUE_REGEXP = /^#\+(\S+):\s*(.*)\R?/ # #+key: value
4
4
 
5
5
  PLUGINS = [
6
- Type::ElementReject, # require "setup"
7
- Type::ElementMdHeader, # ---
8
- Type::ElementMdTitle, # ## foo ##
9
- Type::ElementInclude, # #+include: foo.txt
10
- Type::ElementTitle, # #+title2: foo
11
- Type::ElementWarn, # #+warn: foo
12
- Type::ElementAlert, # #+alert: foo
13
- Type::ElementMethod, # #+name: foo
14
- Type::ElementTable, # |-
15
- Type::ElementSourceBlock, # #+BEGIN_SRC
16
- Type::ElementUncomment, # foo
17
- Type::ElementPartialCode, # 1 + 2 # => 3
18
- Type::ElementElse,
6
+ Formatter::TypeHidden, # #+hidden: true
7
+ Formatter::TypeMdTitle, # ## foo ##
8
+ Formatter::TypeCodeInclude, # #+code_include: path/to/foo.html xml:SAMPLE.xml
9
+ Formatter::TypeRawInclude, # #+raw_include: path/to/file.txt
10
+ Formatter::TypeParseInclude, # #+parse_include: path/to/file.txt
11
+ Formatter::TypeTitle, # #+title2: foo
12
+ Formatter::TypeWarn, # #+warn: foo
13
+ Formatter::TypeAlert, # #+alert: foo
14
+ Formatter::TypeMethod, # #+name: foo
15
+ Formatter::TypeTable, # # |-
16
+ Formatter::TypeSourceBlock, # #+BEGIN_SRC
17
+ Formatter::TypeText, # # foo
18
+ Formatter::TypePartialCode, # 1 + 2 # => 3
19
+ Formatter::TypeElse,
19
20
  ]
20
21
 
21
22
  def initialize(content)
@@ -29,11 +30,11 @@ module Source2MD
29
30
  end
30
31
 
31
32
  def head
32
- @head ||= @content.scan(KEY_VALUE).to_h.symbolize_keys.freeze
33
+ @head ||= @content.scan(KEY_VALUE_REGEXP).to_h.freeze
33
34
  end
34
35
 
35
36
  def body
36
- @body ||= @content.remove(KEY_VALUE).strip.freeze
37
+ @body ||= @content.remove(KEY_VALUE_REGEXP).strip.freeze
37
38
  end
38
39
 
39
40
  private
@@ -1,5 +1,5 @@
1
1
  module Source2MD
2
- module Type
2
+ module Formatter
3
3
  class Base
4
4
  def self.accept?(element)
5
5
  false
@@ -11,10 +11,9 @@ module Source2MD
11
11
  @element = element
12
12
  end
13
13
 
14
- # def to_md
15
- #
16
- # to_md
17
- # end
14
+ def to_md
15
+ raise NotImplementedError, "#{__method__} is not implemented"
16
+ end
18
17
  end
19
18
  end
20
19
  end
@@ -1,14 +1,14 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementAlert < Base
2
+ module Formatter
3
+ class TypeAlert < Base
4
4
  def self.accept?(element)
5
- element.head[:alert]
5
+ element.head["alert"]
6
6
  end
7
7
 
8
8
  def to_md
9
9
  [
10
10
  ":::message alert",
11
- element.head[:alert],
11
+ element.head["alert"],
12
12
  ":::",
13
13
  ] * "\n"
14
14
  end
@@ -1,14 +1,14 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementInclude < Base
2
+ module Formatter
3
+ class TypeCodeInclude < Base
4
4
  def self.accept?(element)
5
- element.head[:include]
5
+ element.head["code_include"]
6
6
  end
7
7
 
8
8
  def initialize(...)
9
9
  super
10
10
 
11
- @user_define_path, @desc = element.head[:include].split(/\s+/, 2)
11
+ @user_define_path, @desc = element.head["code_include"].split(/\s+/, 2)
12
12
  end
13
13
 
14
14
  def to_md
@@ -26,7 +26,7 @@ module Source2MD
26
26
  end
27
27
 
28
28
  def code_block_options
29
- options = element.head.dup
29
+ options = element.head.dup.to_options
30
30
  options[:desc] ||= @desc
31
31
  options[:lang] ||= auto_lang
32
32
  options[:name] ||= path.basename
@@ -1,6 +1,6 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementElse < Base
2
+ module Formatter
3
+ class TypeElse < Base
4
4
  def self.accept?(element)
5
5
  true
6
6
  end
@@ -1,11 +1,12 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementReject < Base
2
+ module Formatter
3
+ class TypeHidden < Base
4
4
  def self.accept?(element)
5
- element.body.match?(/^require.*setup/)
5
+ element.head["hidden"] == "true"
6
6
  end
7
7
 
8
8
  def to_md
9
+ ""
9
10
  end
10
11
  end
11
12
  end
@@ -1,6 +1,6 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementMdTitle < Base
2
+ module Formatter
3
+ class TypeMdTitle < Base
4
4
  def self.accept?(element)
5
5
  element.head.blank? && element.body.match?(/\A(#+)\s+.*\s+\1\z/)
6
6
  end
@@ -0,0 +1,24 @@
1
+ module Source2MD
2
+ module Formatter
3
+ class TypeMethod < Base
4
+ def self.accept?(element)
5
+ element.head["name"]
6
+ end
7
+
8
+ def to_md
9
+ [
10
+ "### #{element.head["name"]} ###",
11
+ element.head["desc"],
12
+ CodeBlock.new(body).to_md,
13
+ element.head["comment"],
14
+ ].compact * "\n\n"
15
+ end
16
+
17
+ private
18
+
19
+ def body
20
+ element.body.gsub(/^\#$/, "")
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,13 @@
1
+ module Source2MD
2
+ module Formatter
3
+ class TypeParseInclude < Base
4
+ def self.accept?(element)
5
+ element.head["parse_include"]
6
+ end
7
+
8
+ def to_md
9
+ Generator.new(files: element.head["parse_include"]).to_md.strip
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,6 +1,6 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementPartialCode < Base
2
+ module Formatter
3
+ class TypePartialCode < Base
4
4
  def self.accept?(element)
5
5
  !element.body.empty?
6
6
  end
@@ -0,0 +1,13 @@
1
+ module Source2MD
2
+ module Formatter
3
+ class TypeRawInclude < Base
4
+ def self.accept?(element)
5
+ element.head["raw_include"]
6
+ end
7
+
8
+ def to_md
9
+ Pathname(element.head["raw_include"]).expand_path.read.strip
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,6 +1,6 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementSourceBlock < Base
2
+ module Formatter
3
+ class TypeSourceBlock < Base
4
4
  def self.accept?(element)
5
5
  element.body.start_with?("#+BEGIN_SRC")
6
6
  end
@@ -1,6 +1,6 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementTable < Base
2
+ module Formatter
3
+ class TypeTable < Base
4
4
  def self.accept?(element)
5
5
  !element.body.empty? && element.body.lines.all? { |e| e.match?(/^# \|.*\|$/) }
6
6
  end
@@ -0,0 +1,31 @@
1
+ module Source2MD
2
+ module Formatter
3
+ class TypeText < Base
4
+ # "# xxx"
5
+ # "#"
6
+ REGEXP = /^#( |$)/
7
+
8
+ def self.accept?(element)
9
+ element.body.lines.all? { |e| e.match?(REGEXP) }
10
+ end
11
+
12
+ def to_md
13
+ body
14
+ end
15
+
16
+ private
17
+
18
+ def body
19
+ s = element.body.remove(REGEXP)
20
+ s = oneline(s)
21
+ end
22
+
23
+ def oneline(s)
24
+ if element.head["oneline"] == "true"
25
+ s = TextHelper.oneline(s)
26
+ end
27
+ s
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,6 +1,6 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementTitle < Base
2
+ module Formatter
3
+ class TypeTitle < Base
4
4
  REGEXP = /\Atitle(\d+)\z/
5
5
 
6
6
  def self.accept?(element)
@@ -1,16 +1,16 @@
1
1
  module Source2MD
2
- module Type
3
- class ElementWarn < Base
2
+ module Formatter
3
+ class TypeWarn < Base
4
4
  def self.accept?(element)
5
5
  if element.body.empty?
6
- element.head[:warn]
6
+ element.head["warn"]
7
7
  end
8
8
  end
9
9
 
10
10
  def to_md
11
11
  [
12
12
  ":::message",
13
- element.head[:warn],
13
+ element.head["warn"],
14
14
  ":::",
15
15
  ] * "\n"
16
16
  end
@@ -11,20 +11,20 @@ module Source2MD
11
11
  puts "write: #{output_file}"
12
12
  end
13
13
 
14
- private
15
-
16
14
  def to_md
17
15
  s = sections.collect(&:to_md) * "\n\n"
18
16
  s = TextHelper.blank_lines_squish(s)
19
17
  s = TextHelper.add_newline_at_end_of_text(s)
20
18
  end
21
19
 
20
+ private
21
+
22
22
  def sections
23
23
  @sections ||= files.collect { |e| Section.new(e.read) }
24
24
  end
25
25
 
26
26
  def files
27
- @files ||= params[:files].collect { |e| Pathname(e) }
27
+ @files ||= Array(params[:files]).collect { |e| Pathname(e).expand_path }
28
28
  end
29
29
 
30
30
  def output_file
@@ -2,7 +2,7 @@ module Source2MD
2
2
  class Scanner
3
3
  SEPARATOR = "\\R{2,}"
4
4
 
5
- MD_CODE_BLOCK_REGEXP = /^# ```.*?^# ```/m
5
+ # MD_CODE_BLOCK_REGEXP = /^# ```.*?^# ```/m
6
6
 
7
7
  BEGIN_KEY = "BEGIN_SRC"
8
8
  END_KEY = "END_SRC"
@@ -11,7 +11,7 @@ module Source2MD
11
11
  NORMAL_BLOCK_REGEXP = /.*?#{SEPARATOR}/m
12
12
 
13
13
  PARAGRAPH_REGEXP = Regexp.union [
14
- MD_CODE_BLOCK_REGEXP,
14
+ # MD_CODE_BLOCK_REGEXP,
15
15
  SOURCE_BLOCK_REGEXP,
16
16
  NORMAL_BLOCK_REGEXP,
17
17
  ]
@@ -3,11 +3,15 @@ module Source2MD
3
3
  extend self
4
4
 
5
5
  def blank_lines_squish(text)
6
- text.gsub(/\n{2,}/, "\n\n")
6
+ text.gsub(/\n{3,}/, "\n\n")
7
7
  end
8
8
 
9
9
  def add_newline_at_end_of_text(text)
10
10
  text.strip + "\n"
11
11
  end
12
+
13
+ def oneline(text)
14
+ text.remove(/\R/)
15
+ end
12
16
  end
13
17
  end
@@ -1,3 +1,3 @@
1
1
  module Source2MD
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -12,7 +12,7 @@ module Source2MD
12
12
  s.should == <<~EOS.strip
13
13
  ```ruby
14
14
  a # => 1
15
-
15
+ #
16
16
  bb # => 2
17
17
  cccc
18
18
  ```
@@ -0,0 +1 @@
1
+ (yaml)
@@ -4,35 +4,35 @@ module Source2MD
4
4
  describe do
5
5
  it "works" do
6
6
  actual = Element.new(<<~EOS).to_md
7
- #+include: #{__dir__}/sample.yml
7
+ #+code_include: #{__dir__}/sample.yml
8
8
  EOS
9
9
  actual.should == <<~EOS.strip
10
10
  ```yml:sample.yml
11
- yaml content
11
+ (yaml)
12
12
  ```
13
13
  EOS
14
14
  end
15
15
 
16
16
  it "works" do
17
17
  actual = Element.new(<<~EOS).to_md
18
- #+include: #{__dir__}/sample.yml
18
+ #+code_include: #{__dir__}/sample.yml
19
19
  #+lang: (lang)
20
20
  #+name: (name)
21
21
  EOS
22
22
  actual.should == <<~EOS.strip
23
23
  ```(lang):(name)
24
- yaml content
24
+ (yaml)
25
25
  ```
26
26
  EOS
27
27
  end
28
28
 
29
29
  it "works" do
30
30
  actual = Element.new(<<~EOS).to_md
31
- #+include: #{__dir__}/sample.yml yaml:filename
31
+ #+code_include: #{__dir__}/sample.yml yaml:filename
32
32
  EOS
33
33
  actual.should == <<~EOS.strip
34
34
  ```yaml:filename
35
- yaml content
35
+ (yaml)
36
36
  ```
37
37
  EOS
38
38
  end
@@ -4,9 +4,10 @@ module Source2MD
4
4
  describe do
5
5
  it "works" do
6
6
  actual = Element.new(<<~EOS).to_md
7
- require "./setup"
7
+ #+hidden: true
8
+ (hidden)
8
9
  EOS
9
- assert { actual.nil? }
10
+ assert { actual == "" }
10
11
  end
11
12
  end
12
13
  end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ module Source2MD
4
+ describe do
5
+ it "works" do
6
+ actual = Element.new(<<~EOS).to_md
7
+ #+parse_include: #{__dir__}/sample.yml
8
+ EOS
9
+ actual.should == <<~EOS.strip
10
+ ```ruby
11
+ (yaml)
12
+ ```
13
+ EOS
14
+ end
15
+ end
16
+ end
@@ -4,14 +4,10 @@ module Source2MD
4
4
  describe do
5
5
  it "works" do
6
6
  actual = Element.new(<<~EOS).to_md
7
- ---
8
- foo: 1
9
- ---
7
+ #+raw_include: #{__dir__}/sample.yml
10
8
  EOS
11
9
  actual.should == <<~EOS.strip
12
- ---
13
- foo: 1
14
- ---
10
+ (yaml)
15
11
  EOS
16
12
  end
17
13
  end
@@ -6,12 +6,24 @@ module Source2MD
6
6
  actual = Element.new(<<~EOS).to_md
7
7
  # - foo
8
8
  # http://example.com/
9
- # - bar: http://example.com/
10
9
  EOS
11
10
  actual.should == <<~EOS.strip
12
11
  - foo
13
12
  http://example.com/
14
- - bar: http://example.com/
13
+ EOS
14
+ end
15
+
16
+ it "works" do
17
+ actual = Element.new(<<~EOS).to_md
18
+ #+oneline: true
19
+ # a
20
+ # b
21
+ #
22
+ # c
23
+ # d
24
+ EOS
25
+ actual.should == <<~EOS.strip
26
+ abcd
15
27
  EOS
16
28
  end
17
29
  end