source2md 0.0.9 → 0.0.11

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 (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -22
  3. data/Rakefile +1 -1
  4. data/doc/0100_top.rb +5 -5
  5. data/doc/0110_code_small.rb +11 -1
  6. data/doc/0120_code_long.rb +9 -1
  7. data/doc/0130_hidden.rb +3 -1
  8. data/doc/0140_code_include.rb +9 -1
  9. data/doc/0150_raw_include.rb +2 -1
  10. data/doc/0160_title.rb +1 -1
  11. data/doc/{0180_table.rb → 0170_table.rb} +1 -1
  12. data/doc/{0190_method.rb → 0180_method.rb} +3 -3
  13. data/doc/{0200_alert.rb → 0190_alert.rb} +8 -1
  14. data/doc/{0210_text.rb → 0200_text.rb} +3 -1
  15. data/doc/{0220_text_oneline.rb → 0210_text_oneline.rb} +2 -1
  16. data/doc/{0230_text_squish.rb → 0220_text_squish.rb} +2 -1
  17. data/doc/{0240_text_hankaku_kana.rb → 0230_text_hankaku_kana.rb} +2 -1
  18. data/doc/{0250_parse_include.rb → 0240_parse_include.rb} +2 -1
  19. data/doc/setup.rb +3 -0
  20. data/examples/cli-test.rs +6 -3
  21. data/examples/cli-test.sh +1 -1
  22. data/lib/source2md/cli.rb +13 -2
  23. data/lib/source2md/code_block.rb +20 -25
  24. data/lib/source2md/element.rb +16 -6
  25. data/lib/source2md/formatter/type_code_include.rb +1 -1
  26. data/lib/source2md/formatter/type_method.rb +15 -7
  27. data/lib/source2md/formatter/{type_md_title.rb → type_nop.rb} +3 -3
  28. data/lib/source2md/formatter/type_raw_include.rb +2 -1
  29. data/lib/source2md/formatter/type_source_block.rb +4 -4
  30. data/lib/source2md/formatter/type_table.rb +5 -5
  31. data/lib/source2md/formatter/type_text.rb +5 -3
  32. data/lib/source2md/generator.rb +1 -1
  33. data/lib/source2md/re.rb +3 -0
  34. data/lib/source2md/regexp_builder.rb +33 -0
  35. data/lib/source2md/scanner.rb +22 -17
  36. data/lib/source2md/text_helper.rb +13 -4
  37. data/lib/source2md/version.rb +1 -1
  38. data/lib/source2md.rb +2 -0
  39. data/spec/code_block_spec.rb +2 -1
  40. data/spec/formatter/type_code_include_spec.rb +3 -3
  41. data/spec/formatter/type_method_spec.rb +3 -2
  42. data/spec/formatter/type_partial_code_spec.rb +1 -1
  43. data/spec/formatter/type_raw_include_spec.rb +1 -1
  44. data/spec/formatter/type_source_block_spec.rb +2 -2
  45. data/spec/formatter/type_table_spec.rb +2 -2
  46. data/spec/formatter/type_text_spec.rb +5 -4
  47. data/spec/scanner_spec.rb +1 -7
  48. data/spec/text_helper_spec.rb +8 -4
  49. metadata +13 -15
  50. data/doc/0170_md_like_title.rb +0 -24
  51. data/examples/type_md_title.rb +0 -15
  52. data/spec/formatter/type_md_title_spec.rb +0 -14
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a69cc0fff709a6adfac13be6784fb107d230200b6797391dfd2c970fab4f48ca
4
- data.tar.gz: 14168fb1b130a4112eb08c4408d6c4bc84b70e5db1b53889201d2c5094343d32
3
+ metadata.gz: 4cbcbb886c48d226f8864af1134aa3c0bdac15dcb2adf2acfd8aadb1d9b62bcc
4
+ data.tar.gz: bdcdb4870afc49aeb7fbb20534a54ba44fd42bd9e1e56c2bb260babe46d9cb74
5
5
  SHA512:
6
- metadata.gz: c539724001175e52377e62c132ed15d622fdc854dfc02b20f6aa6018bc108044832c9bd2617f9c198ff3261c70fc2876da0004a9b862c462d0af3b5b32f39148
7
- data.tar.gz: 12fc867c80e7012e8199264fd7c7c2c90d1a0c6e3ead297317deceaeca66b83176fc23389b2803bf060383341b7a5c2321cb90c2da121d73d6db6806fa36ac26
6
+ metadata.gz: 9fe4bde35b551e33b11a53a10df2941b3828eba7b5e91c470bae0001f26f95ab2ecb006183fb89384be8fe22d3f9b5c461917b1525e3f002800846f19ea2547a
7
+ data.tar.gz: ab48da56d8bf0b4ce3755432f9ed281dbceacf67a8cdbb4663b328f614c73bdcc1680a4e03675d8ba547ff638d054f2249644fca5f52c6812b3e17c6474b523c
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+
2
+
1
3
  # Source2MD: Markdown generator from source code #
2
4
 
3
5
  ## Logic ##
@@ -75,6 +77,14 @@ puts Source2MD::Section.new(<<~EOS).to_md
75
77
  EOS
76
78
  ```
77
79
 
80
+ ```ruby
81
+ puts Source2MD::Section.new(<<~EOS).to_md
82
+ #+BEGIN_SRC hidden!
83
+ p "This paragraph is not displayed"
84
+ #+END_SRC
85
+ EOS
86
+ ```
87
+
78
88
  ### Code include ###
79
89
 
80
90
  Insert inside the code block.
@@ -135,28 +145,6 @@ EOS
135
145
  ### Title Level 3 ###
136
146
  ```
137
147
 
138
- ### Markdown style title ###
139
-
140
- The condition is that there are the same number of sharps on the back.
141
-
142
- ```ruby
143
- puts Source2MD::Section.new(<<~EOS).to_md
144
- # Title Level 1 #
145
-
146
- ## Title Level 2 ##
147
-
148
- ### Title Level 3 ###
149
- EOS
150
- ```
151
-
152
- ```
153
- # Title Level 1 #
154
-
155
- ## Title Level 2 ##
156
-
157
- ### Title Level 3 ###
158
- ```
159
-
160
148
  ### Org-mode table style ###
161
149
 
162
150
  ```ruby
data/Rakefile CHANGED
@@ -11,7 +11,7 @@ namespace :doc do
11
11
  task "generate" do
12
12
  Rake::Task["doc:renum"].execute
13
13
  system "sh", "-vec", <<~EOT, exception: true
14
- .bin/source2md generate --no-debug --no-xmp-out-exclude -o README.md doc/0*
14
+ .bin/source2md generate --no-debug --xmp-out-exclude --prefix_re '^' -o README.md doc/0*
15
15
  EOT
16
16
  end
17
17
 
data/doc/0100_top.rb CHANGED
@@ -1,24 +1,24 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- # Source2MD: Markdown generator from source code #
4
+ #+title1: Source2MD: Markdown generator from source code
5
5
 
6
- ## Logic ##
6
+ #+title2: Logic
7
7
 
8
8
  # 1. Divide the source code into paragraphs using blank lines as separations.
9
9
  # 1. Remove comments from the comment blocks.
10
10
  # 1. Include the code within the code block.
11
11
 
12
- ## Install ##
12
+ #+title2: Install
13
13
 
14
14
  # ```
15
15
  # $ gem i source2md
16
16
  # ```
17
17
 
18
- ## CLI ##
18
+ #+title2: CLI
19
19
 
20
20
  # ```
21
21
  # $ source2md generate -o README.md README.rb
22
22
  # ```
23
23
 
24
- ## Rules ##
24
+ #+title2: Rules
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Code snippet ###
4
+ #+title3: Code snippet
5
5
 
6
6
  # Separated by blank lines.
7
7
 
@@ -24,3 +24,13 @@ EOS
24
24
  # > ```ruby
25
25
  # > hello.call
26
26
  # > ```
27
+ # >> ```ruby
28
+ # >> hello = -> {
29
+ # >> "Hello, world!"
30
+ # >> }
31
+ # >> ```
32
+ # >>
33
+ # >>
34
+ # >> ```ruby
35
+ # >> hello.call
36
+ # >> ```
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Source block with many lines ###
4
+ #+title3: Source block with many lines
5
5
 
6
6
  #+BEGIN_SRC
7
7
  puts Source2MD::Section.new(<<~EOS).to_md
@@ -22,3 +22,11 @@ EOS
22
22
  # >
23
23
  # > hello.call
24
24
  # > ```
25
+ # >> ["/Users/ikeda/src/zenn/source2md/lib/source2md/scanner.rb:14", :to_a, /(?m-ix:(?-mix:(?-mix:^)(?:(?-mix:#|\/\/)))\+BEGIN_SRC.*?(?-mix:(?-mix:^)(?:(?-mix:#|\/\/)))\+END_SRC)|(?m-ix:.*?\R{2,})/]
26
+ # >> ```ruby
27
+ # >> hello = -> {
28
+ # >> "Hello, world!"
29
+ # >> }
30
+ # >>
31
+ # >> hello.call
32
+ # >> ```
data/doc/0130_hidden.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Hide paragraph ###
4
+ #+title3: Hide paragraph
5
5
 
6
6
  #+BEGIN_SRC
7
7
  puts Source2MD::Section.new(<<~EOS).to_md
@@ -17,3 +17,5 @@ puts Source2MD::Section.new(<<~EOS).to_md
17
17
  #+END_SRC
18
18
  EOS
19
19
  #+END_SRC
20
+ # >>
21
+ # >>
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Code include ###
4
+ #+title3: Code include
5
5
 
6
6
  # Insert inside the code block.
7
7
 
@@ -24,3 +24,11 @@ EOS
24
24
  # > <p>Hello</p>
25
25
  # > ```
26
26
  # > ````
27
+ # >> ```html:hello.html
28
+ # >> <p>Hello</p>
29
+ # >> ```
30
+ # >>
31
+ # >>
32
+ # >> ```xml:OUTPUT
33
+ # >> <p>Hello</p>
34
+ # >> ```
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Raw include ###
4
+ #+title3: Raw include
5
5
 
6
6
  #+BEGIN_SRC
7
7
  File.write("/tmp/hello.html", "<p>Hello</p>")
@@ -14,3 +14,4 @@ EOS
14
14
  # > ```
15
15
  # > <p>Hello</p>
16
16
  # > ```
17
+ # >> <p>Hello</p>
data/doc/0160_title.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Title ###
4
+ #+title3: Title
5
5
 
6
6
  # No number limit.
7
7
 
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Org-mode table style ###
4
+ #+title3: Org-mode table style
5
5
 
6
6
  #+BEGIN_SRC
7
7
  puts Source2MD::Section.new(<<~EOS).to_md
@@ -1,19 +1,19 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Explain method simply ###
4
+ #+title3: Explain method simply
5
5
 
6
6
  #+BEGIN_SRC
7
7
  puts Source2MD::Section.new(<<~EOS).to_md
8
8
  #+name: String#size
9
9
  #+desc: Return the number of characters
10
10
  #+comment: Comments about size
11
- "abc".size # => 3
11
+ "abc".size # => 3
12
12
 
13
13
  #+name: String#reverse
14
14
  #+desc: reverse the sequence of characters
15
15
  #+comment: Comments about reverse
16
- "abc".reverse # => "cba"
16
+ "abc".reverse # => "cba"
17
17
  EOS
18
18
  #+END_SRC
19
19
 
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Warning and Alert message ###
4
+ #+title3: Warning and Alert message
5
5
 
6
6
  # Exclusive to Zenn
7
7
 
@@ -24,3 +24,10 @@ EOS
24
24
  # > this is alert message
25
25
  # > :::
26
26
  # > ```
27
+ # >> :::message
28
+ # >> this is warning message
29
+ # >> :::
30
+ # >>
31
+ # >> :::message alert
32
+ # >> this is alert message
33
+ # >> :::
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Raw Text ###
4
+ #+title3: Raw Text
5
5
 
6
6
  # If no rule applies and the text begins with `#`, remove the `#`.
7
7
 
@@ -16,3 +16,5 @@ EOS
16
16
  # > Lorem ipsum dolor sit amet, consectetur adipisicing elit,
17
17
  # > sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
18
18
  # > ```
19
+ # >> Lorem ipsum dolor sit amet, consectetur adipisicing elit,
20
+ # >> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Oneline Text ###
4
+ #+title3: Oneline Text
5
5
 
6
6
  # Using this option allows you to split a line regardless of the markdown library.
7
7
 
@@ -16,3 +16,4 @@ EOS
16
16
  # > ```
17
17
  # > Lorem ipsum dolor sit amet, consectetur adipisicing elit,sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
18
18
  # > ```
19
+ # >> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Squish Text ###
4
+ #+title3: Squish Text
5
5
 
6
6
  # Single spaces for line breaks and consecutive spaces.
7
7
 
@@ -16,3 +16,4 @@ EOS
16
16
  # > ```
17
17
  # > Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
18
18
  # > ```
19
+ # >> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Hankaku Kana ###
4
+ #+title3: Hankaku Kana
5
5
 
6
6
  # JISX0208 Katakana to JISX0201 Katakana.
7
7
 
@@ -13,3 +13,4 @@ EOS
13
13
  #+END_SRC
14
14
 
15
15
  # > アア
16
+ # >> アア
@@ -1,7 +1,7 @@
1
1
  #+hidden: true
2
2
  require "./setup"
3
3
 
4
- ### Parse include ###
4
+ #+title3: Parse include
5
5
 
6
6
  # Paste the results of processing other files with the same rules.
7
7
 
@@ -16,3 +16,4 @@ EOS
16
16
  #+END_SRC
17
17
 
18
18
  # > foo
19
+ # >> foo
data/doc/setup.rb CHANGED
@@ -2,3 +2,6 @@ $LOAD_PATH.unshift("#{__dir__}/../lib")
2
2
  require "source2md"
3
3
  Source2MD.logger.level = :debug if false
4
4
 
5
+ # Source2MD::RE.update do |e|
6
+ # e.prefix_re = %r{^}
7
+ # end
data/examples/cli-test.rs CHANGED
@@ -1,3 +1,6 @@
1
- a
2
- b
3
- c
1
+ //+name: (name)
2
+ //+desc: (desc)
3
+ //+comment: (comment)
4
+ 1; // => 1
5
+ //
6
+ 2; // => 2
data/examples/cli-test.sh CHANGED
@@ -1,2 +1,2 @@
1
1
  #!/bin/sh
2
- source2md generate -d a.rs
2
+ ../.bin/source2md generate -d cli-test.rs
data/lib/source2md/cli.rb CHANGED
@@ -2,7 +2,10 @@ module Source2MD
2
2
  class Cli < Thor
3
3
  class_option :debug, type: :boolean, aliases: "-d", default: false
4
4
  class_option :xmp_out_exclude, type: :boolean, aliases: "-x", default: false
5
- class_option :readonly, type: :boolean, default: true
5
+ class_option :readonly, type: :boolean, aliases: "-r", default: true
6
+ class_option :default_lang, type: :string, aliases: "-l", default: "ruby"
7
+ class_option :prefix_re, type: :string
8
+ class_option :comment_re, type: :string
6
9
 
7
10
  def initialize(...)
8
11
  super
@@ -14,8 +17,16 @@ module Source2MD
14
17
 
15
18
  Source2MD.xmp_out_exclude = options[:xmp_out_exclude]
16
19
  Source2MD.readonly = options[:readonly]
20
+ Source2MD.default_lang = options[:default_lang]
17
21
 
18
- tp Source2MD.config
22
+ Source2MD::RE.update do |e|
23
+ if v = options[:prefix_re]
24
+ e.prefix_re = v
25
+ end
26
+ if v = options[:comment_re]
27
+ e.comment_re = v
28
+ end
29
+ end
19
30
  end
20
31
 
21
32
  # default_command :generate
@@ -1,24 +1,22 @@
1
1
  module Source2MD
2
2
  class CodeBlock
3
3
  PADDING_KEEP = 2
4
- MARK = /(?:#|\/\/) =>/
4
+ ARROW_MARK = %r{(?:#{RE.comment_re}) =>}
5
5
 
6
- attr_accessor :code
7
-
8
- def initialize(code, options = {})
9
- @code = code
6
+ def initialize(text, options = {})
7
+ @text = text
10
8
  @options = {
11
- :lang => "ruby",
9
+ :lang => nil,
12
10
  # :single_sharp_replace_to_blank_line => false,
13
11
  }.merge(options)
14
12
  end
15
13
 
16
14
  def to_md
17
15
  [
18
- "```#{code_block_head}",
16
+ "```#{code_block_head}\n",
19
17
  normalized_code,
20
- "```",
21
- ] * "\n"
18
+ "```\n",
19
+ ].join
22
20
  end
23
21
 
24
22
  private
@@ -28,7 +26,7 @@ module Source2MD
28
26
  if s = @options[:desc]
29
27
  o << s
30
28
  else
31
- if s = @options[:lang]
29
+ if s = lang
32
30
  o << s
33
31
  end
34
32
  if s = @options[:name]
@@ -40,25 +38,22 @@ module Source2MD
40
38
  end
41
39
 
42
40
  def normalized_code
43
- lines.collect(&method(:normalize)) * "\n"
41
+ lines.collect(&method(:normalize)).join
44
42
  end
45
43
 
46
44
  def normalize(line)
47
45
  # if @options[:single_sharp_replace_to_blank_line]
48
46
  # line = single_sharp_replace_to_blank_line(line)
49
47
  # end
50
- if @options[:lang] == "ruby"
51
- line = comment_mark_justfiy(line)
52
- end
53
- line
48
+ comment_re_justfiy(line)
54
49
  end
55
50
 
56
51
  # def single_sharp_replace_to_blank_line(line)
57
52
  # line.gsub(/\A#\z/, "")
58
53
  # end
59
54
 
60
- def comment_mark_justfiy(line)
61
- line.gsub(/(.*?)\s*(#{MARK})(.*)/) {
55
+ def comment_re_justfiy(line)
56
+ line.gsub(/(.*?)\s*(#{ARROW_MARK})(.*)/) {
62
57
  a, b, c = Regexp.last_match.captures
63
58
  space = " " * (PADDING_KEEP + (max - a.size))
64
59
  [a, space, b, c].join
@@ -66,24 +61,24 @@ module Source2MD
66
61
  end
67
62
 
68
63
  def raw_lines
69
- @raw_lines ||= code.lines.collect(&:rstrip)
64
+ @raw_lines ||= @text.lines
70
65
  end
71
66
 
72
67
  def lines
73
- @lines ||= yield_self do
74
- min = raw_lines.collect { |e| e.slice(/^\s*/).size }.min
75
- re = /^\s{#{min}}/
76
- raw_lines.collect { |e| e.remove(re) }
77
- end
68
+ @lines ||= TextHelper.space_prefix_remove(@text).lines
78
69
  end
79
70
 
80
71
  def max
81
72
  @max ||= yield_self do
82
73
  av = lines
83
- av = av.find_all { |e| e.match?(MARK) }
84
- av = av.collect { |e| e.gsub(/\s*#{MARK}.*/, "").size }
74
+ av = av.find_all { |e| e.match?(ARROW_MARK) }
75
+ av = av.collect { |e| e.gsub(/\s*#{ARROW_MARK}.*\R/, "").size }
85
76
  av.max
86
77
  end
87
78
  end
79
+
80
+ def lang
81
+ @options[:lang] || ENV["DEFAULT_LANG"] || Source2MD.default_lang
82
+ end
88
83
  end
89
84
  end
@@ -1,10 +1,7 @@
1
1
  module Source2MD
2
2
  class Element
3
- KEY_VALUE_REGEXP = /^(?:#|\/\/)\+(\S+):\s*(.*)\R?/ # #+key: value
4
-
5
3
  PLUGINS = [
6
4
  Formatter::TypeHidden, # #+hidden: true
7
- Formatter::TypeMdTitle, # ## foo ##
8
5
  Formatter::TypeCodeInclude, # #+code_include: path/to/foo.html xml:SAMPLE.xml
9
6
  Formatter::TypeRawInclude, # #+raw_include: path/to/file.txt
10
7
  Formatter::TypeParseInclude, # #+parse_include: path/to/file.txt
@@ -30,17 +27,25 @@ module Source2MD
30
27
  end
31
28
 
32
29
  def head
33
- @head ||= @content.scan(KEY_VALUE_REGEXP).to_h.freeze
30
+ @head ||= @content.scan(key_value_regexp).to_h.freeze
34
31
  end
35
32
 
36
33
  def body
37
- @body ||= @content.remove(KEY_VALUE_REGEXP).rstrip.freeze
34
+ @body ||= @content.remove(key_value_regexp).freeze
38
35
  end
39
36
 
40
37
  private
41
38
 
42
39
  def support_klass
43
- @support_klass ||= PLUGINS.find { |e| e.accept?(self) }
40
+ @support_klass ||= yield_self do
41
+ Source2MD.logger.debug { "head: #{head.inspect}" }
42
+ Source2MD.logger.debug { "body: #{body.inspect}" }
43
+ PLUGINS.find do |e|
44
+ e.accept?(self).tap do |result|
45
+ Source2MD.logger.debug { "#{e} => #{result}" }
46
+ end
47
+ end
48
+ end
44
49
  end
45
50
 
46
51
  def debug_log(object)
@@ -58,5 +63,10 @@ module Source2MD
58
63
  s.gsub(/^/, "> ")
59
64
  end
60
65
  end
66
+
67
+ # #+key: value
68
+ def key_value_regexp
69
+ /^#{RE.meta_re}\+(\S+):\s*(.*)\R?/
70
+ end
61
71
  end
62
72
  end
@@ -22,7 +22,7 @@ module Source2MD
22
22
  end
23
23
 
24
24
  def body
25
- path.read.strip
25
+ TextHelper.eol_enter(path.read)
26
26
  end
27
27
 
28
28
  def code_block_options
@@ -6,18 +6,26 @@ module Source2MD
6
6
  end
7
7
 
8
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"
9
+ o = []
10
+ o << "### #{element.head["name"]} ###\n"
11
+ if v = element.head["desc"]
12
+ o << "\n"
13
+ o << v + "\n"
14
+ o << "\n"
15
+ end
16
+ o << CodeBlock.new(body).to_md
17
+ if v = element.head["comment"]
18
+ o << "\n"
19
+ o << v + "\n"
20
+ o << "\n"
21
+ end
22
+ o.join
15
23
  end
16
24
 
17
25
  private
18
26
 
19
27
  def body
20
- element.body.gsub(/^\#$/, "")
28
+ element.body.gsub(%r{(#{RE.comment_re})$}, "")
21
29
  end
22
30
  end
23
31
  end
@@ -1,12 +1,12 @@
1
1
  module Source2MD
2
2
  module Formatter
3
- class TypeMdTitle < Base
3
+ class TypeNop < Base
4
4
  def self.accept?(element)
5
- element.head.blank? && element.body.match?(/\A(#+)\s+.*\s+\1\z/)
5
+ element.head["nop"]
6
6
  end
7
7
 
8
8
  def to_md
9
- element.body
9
+ ""
10
10
  end
11
11
  end
12
12
  end
@@ -6,7 +6,8 @@ module Source2MD
6
6
  end
7
7
 
8
8
  def to_md
9
- Pathname(element.head["raw_include"]).expand_path.read.strip
9
+ s = Pathname(element.head["raw_include"]).expand_path.read
10
+ TextHelper.eol_enter(s)
10
11
  end
11
12
  end
12
13
  end
@@ -2,7 +2,7 @@ module Source2MD
2
2
  module Formatter
3
3
  class TypeSourceBlock < Base
4
4
  def self.accept?(element)
5
- element.body.start_with?("#+BEGIN_SRC")
5
+ element.body.match?(%r{#{RE.meta_re}\+BEGIN_SRC})
6
6
  end
7
7
 
8
8
  def to_md
@@ -15,12 +15,12 @@ module Source2MD
15
15
  private
16
16
 
17
17
  def body
18
- element.body.match(/#\+BEGIN_SRC.*?\R(.*)#\+END_SRC/m).captures.first.strip
18
+ element.body.match(%r{#{RE.meta_re}\+BEGIN_SRC.*?\R(.*)#{RE.meta_re}\+END_SRC}m).captures.first
19
19
  end
20
20
 
21
21
  def code_block_desc
22
- if md = element.body.match(/#\+BEGIN_SRC(.+)/)
23
- md.captures.first.strip
22
+ if md = element.body.match(%r{#{RE.meta_re}\+BEGIN_SRC (.+)\R})
23
+ md.captures.first
24
24
  end
25
25
  end
26
26
 
@@ -2,15 +2,15 @@ module Source2MD
2
2
  module Formatter
3
3
  class TypeTable < Base
4
4
  def self.accept?(element)
5
- !element.body.empty? && element.body.lines.all? { |e| e.match?(/^# \|.*\|$/) }
5
+ !element.body.empty? && element.body.lines.all? { |e| e.match?(%r{#{RE.meta_re} \|.*\|$}) }
6
6
  end
7
7
 
8
8
  def to_md
9
9
  element.body
10
- .remove(/^# /)
11
- .remove(/\A\|-.*?-\|\R?/)
12
- .remove(/^\|-.*?-\|\z/)
13
- .gsub(/-\+-/, "-|-").strip
10
+ .remove(%r{#{RE.meta_re} })
11
+ .remove(/\A\|-.*?-\|\R/) # top
12
+ .remove(/^\|-.*?-\|\R\z/) # bottom
13
+ .gsub(/-\+-/, "-|-")
14
14
  end
15
15
  end
16
16
  end
@@ -3,10 +3,12 @@ module Source2MD
3
3
  class TypeText < Base
4
4
  # "# xxx"
5
5
  # "#"
6
- REGEXP = /^#( |$)/
6
+ REGEXP = %r{#{RE.meta_re}( |$)}
7
7
 
8
8
  def self.accept?(element)
9
- element.body.lines.all? { |e| e.match?(REGEXP) }
9
+ if element.body.present?
10
+ element.body.lines.all? { |e| e.match?(REGEXP) }
11
+ end
10
12
  end
11
13
 
12
14
  def to_md
@@ -26,7 +28,7 @@ module Source2MD
26
28
  if element.head["hankaku_kana"] == "true"
27
29
  s = TextHelper.hankaku_kana(s)
28
30
  end
29
- s
31
+ TextHelper.eol_enter(s)
30
32
  end
31
33
  end
32
34
  end
@@ -26,7 +26,7 @@ module Source2MD
26
26
  def to_md
27
27
  s = sections.collect(&:to_md) * "\n\n"
28
28
  s = TextHelper.blank_lines_squish(s)
29
- s = TextHelper.add_newline_at_end_of_text(s)
29
+ s = TextHelper.eol_enter(s)
30
30
  end
31
31
 
32
32
  private
@@ -0,0 +1,3 @@
1
+ module Source2MD
2
+ RE = RegexpBuilder.new
3
+ end
@@ -0,0 +1,33 @@
1
+ module Source2MD
2
+ class RegexpBuilder
3
+ attr_accessor :prefix_re
4
+ attr_accessor :comment_re
5
+
6
+ def initialize
7
+ update do |e|
8
+ e.prefix_re = %r{^\s*}
9
+ e.comment_re = %r{#|//}
10
+ end
11
+ end
12
+
13
+ def update(&block)
14
+ yield self
15
+ reset
16
+ end
17
+
18
+ def meta_re
19
+ @meta_re ||= %r{#{prefix_re}(?:#{comment_re})}
20
+ end
21
+
22
+ def stdout_re
23
+ @stdout_re ||= %r{^(?:#{comment_re}) >>.*$}
24
+ end
25
+
26
+ private
27
+
28
+ def reset
29
+ @meta_re = nil
30
+ @stdout_re = nil
31
+ end
32
+ end
33
+ end
@@ -1,31 +1,36 @@
1
1
  module Source2MD
2
2
  class Scanner
3
- SEPARATOR = "\\R{2,}"
4
-
5
- SRC_BEGIN_KEY = "BEGIN_SRC"
6
- SRC_END_KEY = "END_SRC"
7
- SRC_BLOCK_RE = /^#\+#{SRC_BEGIN_KEY}.*?^#\+#{SRC_END_KEY}/m
8
-
9
- NORMAL_BLOCK_RE = /.*?#{SEPARATOR}/m
10
-
11
- PARAGRAPH_RE = Regexp.union [
12
- SRC_BLOCK_RE,
13
- NORMAL_BLOCK_RE,
14
- ]
15
-
16
3
  def initialize(content)
17
4
  @content = content
18
5
  end
19
6
 
20
7
  def to_a
21
8
  v = @content
9
+ v = v.rstrip + "\n\n"
22
10
  if Source2MD.xmp_out_exclude
23
- v = v.remove(/^# >>.*$/)
11
+ v = v.remove(RE.stdout_re)
24
12
  end
25
- v = v + "\n\n"
26
- v = v.scan(PARAGRAPH_RE)
27
- v = v.collect(&:rstrip)
13
+ v = v.scan(paragraph_re)
14
+ v = v.collect { |e| e.rstrip + "\n" }
28
15
  v = v.find_all(&:present?)
29
16
  end
17
+
18
+ private
19
+
20
+ def paragraph_re
21
+ Regexp.union(src_block_re, normal_block_re)
22
+ end
23
+
24
+ def src_block_re
25
+ %r{#{RE.meta_re}\+BEGIN_SRC.*?#{RE.meta_re}\+END_SRC}m
26
+ end
27
+
28
+ def normal_block_re
29
+ /.*?#{separator}/m
30
+ end
31
+
32
+ def separator
33
+ "\\R{2,}"
34
+ end
30
35
  end
31
36
  end
@@ -6,10 +6,6 @@ module Source2MD
6
6
  text.gsub(/\n{3,}/, "\n\n")
7
7
  end
8
8
 
9
- def add_newline_at_end_of_text(text)
10
- text.strip + "\n"
11
- end
12
-
13
9
  def oneline(text)
14
10
  text.remove(/\R+/)
15
11
  end
@@ -21,5 +17,18 @@ module Source2MD
21
17
  def hankaku_kana(text)
22
18
  NKF.nkf("-wxZ4", text)
23
19
  end
20
+
21
+ def indent(text, size = 2)
22
+ text.gsub(/^/, " " * size)
23
+ end
24
+
25
+ def eol_enter(text)
26
+ text.rstrip + "\n"
27
+ end
28
+
29
+ def space_prefix_remove(text)
30
+ min = text.lines.reject(&:blank?).collect { |e| e.slice(/^\s*/).size }.min
31
+ text.remove(/^ {#{min}}/)
32
+ end
24
33
  end
25
34
  end
@@ -1,3 +1,3 @@
1
1
  module Source2MD
2
- VERSION = "0.0.9"
2
+ VERSION = "0.0.11"
3
3
  end
data/lib/source2md.rb CHANGED
@@ -15,6 +15,7 @@ module Source2MD
15
15
  config_accessor(:debug) { false }
16
16
  config_accessor(:xmp_out_exclude) { false }
17
17
  config_accessor(:readonly) { true }
18
+ config_accessor(:default_lang) { "ruby" }
18
19
  end
19
20
 
20
21
  require "zeitwerk"
@@ -22,6 +23,7 @@ loader = Zeitwerk::Loader.for_gem
22
23
  loader.ignore("#{__dir__}/source2md/logger.rb")
23
24
  loader.ignore("#{__dir__}/source2md/**/_*.rb")
24
25
  loader.inflector.inflect("source2md" => "Source2MD")
26
+ loader.inflector.inflect("re" => "RE")
25
27
  loader.log! if false
26
28
  loader.setup
27
29
 
@@ -9,7 +9,8 @@ module Source2MD
9
9
  bb # => 2
10
10
  cccc
11
11
  EOS
12
- s.should == <<~EOS.strip
12
+
13
+ s.should == <<~EOS
13
14
  ```ruby
14
15
  a # => 1
15
16
  #
@@ -6,7 +6,7 @@ module Source2MD
6
6
  actual = Element.new(<<~EOS).to_md
7
7
  #+code_include: #{__dir__}/sample.yml
8
8
  EOS
9
- actual.should == <<~EOS.strip
9
+ actual.should == <<~EOS
10
10
  ```yml:sample.yml
11
11
  (yaml)
12
12
  ```
@@ -19,7 +19,7 @@ EOS
19
19
  #+lang: (lang)
20
20
  #+name: (name)
21
21
  EOS
22
- actual.should == <<~EOS.strip
22
+ actual.should == <<~EOS
23
23
  ```(lang):(name)
24
24
  (yaml)
25
25
  ```
@@ -30,7 +30,7 @@ EOS
30
30
  actual = Element.new(<<~EOS).to_md
31
31
  #+code_include: #{__dir__}/sample.yml yaml:filename
32
32
  EOS
33
- actual.should == <<~EOS.strip
33
+ actual.should == <<~EOS
34
34
  ```yaml:filename
35
35
  (yaml)
36
36
  ```
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  module Source2MD
4
4
  describe do
5
5
  it "works" do
6
- actual = Element.new(<<~EOS).to_md
6
+ actual = Element.new(TextHelper.indent(<<~EOS)).to_md
7
7
  #+name: (name)
8
8
  #+desc: (desc)
9
9
  #+comment: (comment)
@@ -11,7 +11,7 @@ module Source2MD
11
11
  #
12
12
  (code2)
13
13
  EOS
14
- actual.should == <<~EOS.strip
14
+ actual.should == <<~EOS
15
15
  ### (name) ###
16
16
 
17
17
  (desc)
@@ -23,6 +23,7 @@ EOS
23
23
  ```
24
24
 
25
25
  (comment)
26
+
26
27
  EOS
27
28
  end
28
29
  end
@@ -6,7 +6,7 @@ module Source2MD
6
6
  actual = Element.new(<<~EOS).to_md
7
7
  (foo)
8
8
  EOS
9
- actual.should == <<~EOS.strip
9
+ actual.should == <<~EOS
10
10
  ```ruby
11
11
  (foo)
12
12
  ```
@@ -6,7 +6,7 @@ module Source2MD
6
6
  actual = Element.new(<<~EOS).to_md
7
7
  #+raw_include: #{__dir__}/sample.yml
8
8
  EOS
9
- actual.should == <<~EOS.strip
9
+ actual.should == <<~EOS
10
10
  (yaml)
11
11
  EOS
12
12
  end
@@ -8,7 +8,7 @@ module Source2MD
8
8
  (foo)
9
9
  #+END_SRC
10
10
  EOS
11
- actual.should == <<~EOS.strip
11
+ actual.should == <<~EOS
12
12
  ```ruby
13
13
  (foo)
14
14
  ```
@@ -21,7 +21,7 @@ module Source2MD
21
21
  (foo)
22
22
  #+END_SRC
23
23
  EOS
24
- actual.should == <<~EOS.strip
24
+ actual.should == <<~EOS
25
25
  ```diff xxx:yyy
26
26
  (foo)
27
27
  ```
@@ -11,7 +11,7 @@ module Source2MD
11
11
  # | d | d | d |
12
12
  # |---+---+---|
13
13
  EOS
14
- actual.should == <<~EOS.strip
14
+ actual.should == <<~EOS
15
15
  | h | h | h |
16
16
  |---|---|---|
17
17
  | d | d | d |
@@ -25,7 +25,7 @@ EOS
25
25
  # |---+---+---|
26
26
  # | d | d | d |
27
27
  EOS
28
- actual.should == <<~EOS.strip
28
+ actual.should == <<~EOS
29
29
  | h | h | h |
30
30
  |---|---|---|
31
31
  | d | d | d |
@@ -3,11 +3,12 @@ require "spec_helper"
3
3
  module Source2MD
4
4
  describe do
5
5
  it "works" do
6
+ # Source2MD.logger.level = :debug
6
7
  actual = Element.new(<<~EOS).to_md
7
8
  # - foo
8
9
  # http://example.com/
9
10
  EOS
10
- actual.should == <<~EOS.strip
11
+ actual.should == <<~EOS
11
12
  - foo
12
13
  http://example.com/
13
14
  EOS
@@ -22,7 +23,7 @@ EOS
22
23
  # c
23
24
  # d
24
25
  EOS
25
- actual.should == <<~EOS.strip
26
+ actual.should == <<~EOS
26
27
  abcd
27
28
  EOS
28
29
  end
@@ -36,7 +37,7 @@ EOS
36
37
  # c
37
38
  # d
38
39
  EOS
39
- actual.should == <<~EOS.strip
40
+ actual.should == <<~EOS
40
41
  a b c d
41
42
  EOS
42
43
  end
@@ -46,7 +47,7 @@ EOS
46
47
  #+hankaku_kana: true
47
48
  # アア
48
49
  EOS
49
- actual.should == "アア"
50
+ actual.should == "アア\n"
50
51
  end
51
52
  end
52
53
  end
data/spec/scanner_spec.rb CHANGED
@@ -21,13 +21,7 @@ module Source2MD
21
21
  foo
22
22
  EOS
23
23
 
24
- ary # => ["foo", "#+BEGIN_SRC\n\nfoo\n\n#+END_SRC", "foo", "#+BEGIN_SRC\nfoo\n#+END_SRC", "foo"]
25
- assert { ary == ["foo", "#+BEGIN_SRC\n\nfoo\n\n#+END_SRC", "foo", "#+BEGIN_SRC\nfoo\n#+END_SRC", "foo"] }
24
+ assert { ary == ["foo\n", "#+BEGIN_SRC\n\nfoo\n\n#+END_SRC\n", "foo\n", "#+BEGIN_SRC\nfoo\n#+END_SRC\n", "foo\n"] }
26
25
  end
27
26
  end
28
27
  end
29
- # >> .
30
- # >>
31
- # >> Finished in 0.00793 seconds (files took 0.26816 seconds to load)
32
- # >> 1 example, 0 failures
33
- # >>
@@ -6,14 +6,18 @@ module Source2MD
6
6
  assert { TextHelper.blank_lines_squish("A\nB\n\nC\n\n\nD") == "A\nB\n\nC\n\nD" }
7
7
  end
8
8
 
9
- it "add_newline_at_end_of_text" do
10
- assert { TextHelper.add_newline_at_end_of_text("A") == "A\n" }
11
- assert { TextHelper.add_newline_at_end_of_text("A\n") == "A\n" }
12
- assert { TextHelper.add_newline_at_end_of_text("A\n\n") == "A\n" }
9
+ it "eol_enter" do
10
+ assert { TextHelper.eol_enter("A") == "A\n" }
11
+ assert { TextHelper.eol_enter("A\n\n") == "A\n" }
13
12
  end
14
13
 
15
14
  it "hankaku_kana" do
16
15
  assert { TextHelper.hankaku_kana("アア") == "アア" }
17
16
  end
17
+
18
+ it "space_prefix_remove" do
19
+ assert { TextHelper.space_prefix_remove(" a\n b\n") == "a\n b\n" }
20
+ assert { TextHelper.space_prefix_remove(" a\n\n b\n") == "a\n\n b\n" }
21
+ end
18
22
  end
19
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: source2md
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akira Ikeda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-08-09 00:00:00.000000000 Z
11
+ date: 2023-08-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -155,15 +155,14 @@ files:
155
155
  - doc/0140_code_include.rb
156
156
  - doc/0150_raw_include.rb
157
157
  - doc/0160_title.rb
158
- - doc/0170_md_like_title.rb
159
- - doc/0180_table.rb
160
- - doc/0190_method.rb
161
- - doc/0200_alert.rb
162
- - doc/0210_text.rb
163
- - doc/0220_text_oneline.rb
164
- - doc/0230_text_squish.rb
165
- - doc/0240_text_hankaku_kana.rb
166
- - doc/0250_parse_include.rb
158
+ - doc/0170_table.rb
159
+ - doc/0180_method.rb
160
+ - doc/0190_alert.rb
161
+ - doc/0200_text.rb
162
+ - doc/0210_text_oneline.rb
163
+ - doc/0220_text_squish.rb
164
+ - doc/0230_text_hankaku_kana.rb
165
+ - doc/0240_parse_include.rb
167
166
  - doc/setup.rb
168
167
  - examples/cli-test.rs
169
168
  - examples/cli-test.sh
@@ -177,7 +176,6 @@ files:
177
176
  - examples/type_alert.rb
178
177
  - examples/type_code_include.rb
179
178
  - examples/type_hidden.rb
180
- - examples/type_md_title.rb
181
179
  - examples/type_method.rb
182
180
  - examples/type_partial_code.rb
183
181
  - examples/type_source_block.rb
@@ -194,8 +192,8 @@ files:
194
192
  - lib/source2md/formatter/type_code_include.rb
195
193
  - lib/source2md/formatter/type_else.rb
196
194
  - lib/source2md/formatter/type_hidden.rb
197
- - lib/source2md/formatter/type_md_title.rb
198
195
  - lib/source2md/formatter/type_method.rb
196
+ - lib/source2md/formatter/type_nop.rb
199
197
  - lib/source2md/formatter/type_parse_include.rb
200
198
  - lib/source2md/formatter/type_partial_code.rb
201
199
  - lib/source2md/formatter/type_raw_include.rb
@@ -206,6 +204,8 @@ files:
206
204
  - lib/source2md/formatter/type_warn.rb
207
205
  - lib/source2md/generator.rb
208
206
  - lib/source2md/logger.rb
207
+ - lib/source2md/re.rb
208
+ - lib/source2md/regexp_builder.rb
209
209
  - lib/source2md/scanner.rb
210
210
  - lib/source2md/section.rb
211
211
  - lib/source2md/tasks/about.rake
@@ -219,7 +219,6 @@ files:
219
219
  - spec/formatter/type_alert_spec.rb
220
220
  - spec/formatter/type_code_include_spec.rb
221
221
  - spec/formatter/type_hidden_spec.rb
222
- - spec/formatter/type_md_title_spec.rb
223
222
  - spec/formatter/type_method_spec.rb
224
223
  - spec/formatter/type_parse_include_spec.rb
225
224
  - spec/formatter/type_partial_code_spec.rb
@@ -261,7 +260,6 @@ test_files:
261
260
  - spec/formatter/type_alert_spec.rb
262
261
  - spec/formatter/type_code_include_spec.rb
263
262
  - spec/formatter/type_hidden_spec.rb
264
- - spec/formatter/type_md_title_spec.rb
265
263
  - spec/formatter/type_method_spec.rb
266
264
  - spec/formatter/type_parse_include_spec.rb
267
265
  - spec/formatter/type_partial_code_spec.rb
@@ -1,24 +0,0 @@
1
- #+hidden: true
2
- require "./setup"
3
-
4
- ### Markdown style title ###
5
-
6
- # The condition is that there are the same number of sharps on the back.
7
-
8
- #+BEGIN_SRC
9
- puts Source2MD::Section.new(<<~EOS).to_md
10
- # Title Level 1 #
11
-
12
- ## Title Level 2 ##
13
-
14
- ### Title Level 3 ###
15
- EOS
16
- #+END_SRC
17
-
18
- # ```
19
- # # Title Level 1 #
20
- #
21
- # ## Title Level 2 ##
22
- #
23
- # ### Title Level 3 ###
24
- # ```
@@ -1,15 +0,0 @@
1
- #+hidden: true
2
- require "./setup"
3
-
4
- puts Source2MD::Element.new(<<~EOS).to_md
5
- ## foo ##
6
- EOS
7
- # >> > -------------------------------------------------------------------------------- Source2MD::Part::TypeMdTitle
8
- # >> > {}
9
- # >> > ------------------------------------------------------------ in
10
- # >> > ## foo ##
11
- # >> >
12
- # >> > ------------------------------------------------------------ out
13
- # >> > ## foo ##
14
- # >> > ------------------------------------------------------------
15
- # >> ## foo ##
@@ -1,14 +0,0 @@
1
- require "spec_helper"
2
-
3
- module Source2MD
4
- describe do
5
- it "works" do
6
- actual = Element.new(<<~EOS).to_md
7
- ## foo ##
8
- EOS
9
- actual.should == <<~EOS.strip
10
- ## foo ##
11
- EOS
12
- end
13
- end
14
- end