source2md 0.0.8 → 0.0.10

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 (37) hide show
  1. checksums.yaml +4 -4
  2. data/examples/cli-test.rs +6 -0
  3. data/examples/cli-test.sh +2 -0
  4. data/examples/type_alert.rb +10 -14
  5. data/examples/type_partial_code.rb +20 -16
  6. data/examples/type_text.rb +6 -6
  7. data/examples/type_title.rb +11 -19
  8. data/examples/type_warn.rb +11 -15
  9. data/lib/source2md/cli.rb +4 -4
  10. data/lib/source2md/code_block.rb +20 -17
  11. data/lib/source2md/element.rb +11 -4
  12. data/lib/source2md/formatter/type_code_include.rb +1 -1
  13. data/lib/source2md/formatter/type_method.rb +15 -7
  14. data/lib/source2md/formatter/{type_md_title.rb → type_nop.rb} +3 -3
  15. data/lib/source2md/formatter/type_raw_include.rb +2 -1
  16. data/lib/source2md/formatter/type_source_block.rb +4 -4
  17. data/lib/source2md/formatter/type_table.rb +5 -5
  18. data/lib/source2md/formatter/type_text.rb +5 -3
  19. data/lib/source2md/generator.rb +18 -8
  20. data/lib/source2md/scanner.rb +4 -4
  21. data/lib/source2md/text_helper.rb +13 -4
  22. data/lib/source2md/version.rb +1 -1
  23. data/lib/source2md.rb +1 -0
  24. data/output.md +13 -0
  25. data/spec/code_block_spec.rb +2 -1
  26. data/spec/formatter/type_code_include_spec.rb +3 -3
  27. data/spec/formatter/type_method_spec.rb +3 -2
  28. data/spec/formatter/type_partial_code_spec.rb +1 -1
  29. data/spec/formatter/type_raw_include_spec.rb +1 -1
  30. data/spec/formatter/type_source_block_spec.rb +2 -2
  31. data/spec/formatter/type_table_spec.rb +2 -2
  32. data/spec/formatter/type_text_spec.rb +5 -4
  33. data/spec/scanner_spec.rb +18 -1
  34. data/spec/text_helper_spec.rb +8 -4
  35. metadata +6 -6
  36. data/examples/type_md_title.rb +0 -15
  37. 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: f07581e2e1a43897fbabf84171220745ae7ca72f96a19fd0f56e9aac6e73bd5d
4
- data.tar.gz: 62e064eaf58861b1abf0b04739e54715a2efbcc74ced61fb0c949e232bf6e3a0
3
+ metadata.gz: 2ac00b7cc8674c792566d8d08c5d2f1b8f5cf755ceca1479b2ac8248ad6dac3e
4
+ data.tar.gz: e7c61a968ca24419cc5a62c86437388f77cd865895ac038a5565b1a4ab4c180d
5
5
  SHA512:
6
- metadata.gz: b426db58ee1eda88c7c9174867cdb6dd9e67d951605a74dc0c2fbcaf8d3464819a84042822fc7d99d78513762be1c3de491af6a2f61dd2d755f8a268fcd58bde
7
- data.tar.gz: 2d97f41bc6ab44b7a2019c2008a4845f879c7b91e82a68ed5fc98e158eebb9a660fe7a34ebfc9a7ef2551c389fc0a84360bc67bd57de56e465900f717a6544c1
6
+ metadata.gz: 25222fde61442a0fceba5daa0e0ff09e7343c0fcacf7123670d85ee66f3a4df50533ec40b211b851eea0870bf64f781b9dd283dc26650a1d7a004d27a95dd513
7
+ data.tar.gz: c89ccfdb613ee1a8fa95eef6a1524be3fe7534a2e5c2fd18fceba4895f87711febc078292f4b974c1ae7a525d75af8e2e528c10b59a55678c7f2730814776077
@@ -0,0 +1,6 @@
1
+ //+name: (name)
2
+ //+desc: (desc)
3
+ //+comment: (comment)
4
+ 1; // => 1
5
+ //
6
+ 2; // => 2
@@ -0,0 +1,2 @@
1
+ #!/bin/sh
2
+ ../.bin/source2md generate -d cli-test.rs
@@ -4,20 +4,16 @@ require "./setup"
4
4
  puts Source2MD::Element.new(<<~EOS).to_md
5
5
  #+alert: (foo)
6
6
  EOS
7
- # >> --------------------------------------------------------------------------------
8
- # >> Source2MD::Part::TypeAlert
9
- # >>
10
- # >> head:
11
- # >> {:alert=>"(foo)"}
12
- # >>
13
- # >> in:
14
- # >>
15
- # >>
16
- # >> out:
17
- # >> :::message alert
18
- # >> (foo)
19
- # >> :::
20
- # >> --------------------------------------------------------------------------------
7
+ # >> > -------------------------------------------------------------------------------- Source2MD::Formatter::TypeAlert
8
+ # >> > {"alert"=>"(foo)"}
9
+ # >> > ------------------------------------------------------------ in
10
+ # >> >
11
+ # >> >
12
+ # >> > ------------------------------------------------------------ out
13
+ # >> > :::message alert
14
+ # >> > (foo)
15
+ # >> > :::
16
+ # >> > ------------------------------------------------------------
21
17
  # >> :::message alert
22
18
  # >> (foo)
23
19
  # >> :::
@@ -2,22 +2,26 @@
2
2
  require "./setup"
3
3
 
4
4
  puts Source2MD::Element.new(<<~EOS).to_md
5
- (foo)
5
+ a
6
+ b
7
+ c
6
8
  EOS
7
- # >> --------------------------------------------------------------------------------
8
- # >> Source2MD::Part::TypePartialCode
9
- # >>
10
- # >> head:
11
- # >> {}
12
- # >>
13
- # >> in:
14
- # >> (foo)
15
- # >>
16
- # >> out:
9
+ # >> > -------------------------------------------------------------------------------- Source2MD::Formatter::TypePartialCode
10
+ # >> > {}
11
+ # >> > ------------------------------------------------------------ in
12
+ # >> > a
13
+ # >> > b
14
+ # >> > c
15
+ # >> >
16
+ # >> > ------------------------------------------------------------ out
17
+ # >> > ```ruby
18
+ # >> > a
19
+ # >> > b
20
+ # >> > c
21
+ # >> > ```
22
+ # >> > ------------------------------------------------------------
17
23
  # >> ```ruby
18
- # >> (foo)
19
- # >> ```
20
- # >> --------------------------------------------------------------------------------
21
- # >> ```ruby
22
- # >> (foo)
24
+ # >> a
25
+ # >> b
26
+ # >> c
23
27
  # >> ```
@@ -25,18 +25,18 @@ puts Source2MD::Element.new(<<~EOS).to_md
25
25
  EOS
26
26
 
27
27
  # >> > -------------------------------------------------------------------------------- Source2MD::Formatter::TypeText
28
- # >> > {"oneline"=>"true"}
28
+ # >> > {"squish"=>"true"}
29
29
  # >> > ------------------------------------------------------------ in
30
30
  # >> > # a
31
31
  # >> > # b
32
32
  # >> > #
33
33
  # >> > # c
34
34
  # >> > # d
35
- # >> >
35
+ # >> >
36
36
  # >> > ------------------------------------------------------------ out
37
- # >> > abcd
37
+ # >> > a b c d
38
38
  # >> > ------------------------------------------------------------
39
- # >> abcd
39
+ # >> a b c d
40
40
  # >> > -------------------------------------------------------------------------------- Source2MD::Formatter::TypeText
41
41
  # >> > {"squish"=>"true"}
42
42
  # >> > ------------------------------------------------------------ in
@@ -45,7 +45,7 @@ EOS
45
45
  # >> > #
46
46
  # >> > # c
47
47
  # >> > # d
48
- # >> >
48
+ # >> >
49
49
  # >> > ------------------------------------------------------------ out
50
50
  # >> > a b c d
51
51
  # >> > ------------------------------------------------------------
@@ -54,7 +54,7 @@ EOS
54
54
  # >> > {"hankaku_kana"=>"true"}
55
55
  # >> > ------------------------------------------------------------ in
56
56
  # >> > # アア
57
- # >> >
57
+ # >> >
58
58
  # >> > ------------------------------------------------------------ out
59
59
  # >> > アア
60
60
  # >> > ------------------------------------------------------------
@@ -4,22 +4,14 @@ require "./setup"
4
4
  puts Source2MD::Element.new(<<~EOS).to_md
5
5
  #+title2: (foo)
6
6
  EOS
7
- # >> title2: (foo)
8
- # >> --------------------------------------------------------------------------------
9
- # >> Source2MD::Part::TypeTitle
10
- # >>
11
- # >> head:
12
- # >> {:title2=>"(foo)"}
13
- # >>
14
- # >> in:
15
- # >>
16
- # >>
17
- # >> out:
18
- # >>
19
- # >> (foo)
20
- # >>
21
- # >> --------------------------------------------------------------------------------
22
- # >> title2: (foo)
23
- # >>
24
- # >> (foo)
25
- # >>
7
+ # >> ## (foo) ##
8
+ # >> > -------------------------------------------------------------------------------- Source2MD::Formatter::TypeTitle
9
+ # >> > {"title2"=>"(foo)"}
10
+ # >> > ------------------------------------------------------------ in
11
+ # >> >
12
+ # >> >
13
+ # >> > ------------------------------------------------------------ out
14
+ # >> > ## (foo) ##
15
+ # >> > ------------------------------------------------------------
16
+ # >> ## (foo) ##
17
+ # >> ## (foo) ##
@@ -2,22 +2,18 @@
2
2
  require "./setup"
3
3
 
4
4
  puts Source2MD::Element.new(<<~EOS).to_md
5
- #+info: (foo)
5
+ #+warn: (foo)
6
6
  EOS
7
- # >> --------------------------------------------------------------------------------
8
- # >> Source2MD::Part::ElementInfo
9
- # >>
10
- # >> head:
11
- # >> {:info=>"(foo)"}
12
- # >>
13
- # >> in:
14
- # >>
15
- # >>
16
- # >> out:
17
- # >> :::message
18
- # >> (foo)
19
- # >> :::
20
- # >> --------------------------------------------------------------------------------
7
+ # >> > -------------------------------------------------------------------------------- Source2MD::Formatter::TypeWarn
8
+ # >> > {"warn"=>"(foo)"}
9
+ # >> > ------------------------------------------------------------ in
10
+ # >> >
11
+ # >> >
12
+ # >> > ------------------------------------------------------------ out
13
+ # >> > :::message
14
+ # >> > (foo)
15
+ # >> > :::
16
+ # >> > ------------------------------------------------------------
21
17
  # >> :::message
22
18
  # >> (foo)
23
19
  # >> :::
data/lib/source2md/cli.rb CHANGED
@@ -2,7 +2,8 @@ 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"
6
7
 
7
8
  def initialize(...)
8
9
  super
@@ -14,15 +15,14 @@ module Source2MD
14
15
 
15
16
  Source2MD.xmp_out_exclude = options[:xmp_out_exclude]
16
17
  Source2MD.readonly = options[:readonly]
17
-
18
- tp Source2MD.config
18
+ Source2MD.default_lang = options[:default_lang]
19
19
  end
20
20
 
21
21
  # default_command :generate
22
22
 
23
23
  map "g" => :generate
24
24
  desc "generate [files]", "Markdown generation"
25
- option :output_file, type: :string, aliases: "-o", default: "output.md"
25
+ option :output_file, type: :string, aliases: "-o", default: nil
26
26
  def generate(*files)
27
27
  Generator.new(options.to_options.merge(files: files)).call
28
28
  end
@@ -1,24 +1,22 @@
1
1
  module Source2MD
2
2
  class CodeBlock
3
3
  PADDING_KEEP = 2
4
- MARK = /(?:#|\/\/) =>/
4
+ MARK = %r{(?:#|//) =>}
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,17 +38,14 @@ 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_mark_justfiy(line)
54
49
  end
55
50
 
56
51
  # def single_sharp_replace_to_blank_line(line)
@@ -65,17 +60,25 @@ module Source2MD
65
60
  }
66
61
  end
67
62
 
63
+ def raw_lines
64
+ @raw_lines ||= @text.lines
65
+ end
66
+
68
67
  def lines
69
- @lines ||= code.strip.lines.collect(&:rstrip)
68
+ @lines ||= TextHelper.space_prefix_remove(@text).lines
70
69
  end
71
70
 
72
71
  def max
73
72
  @max ||= yield_self do
74
73
  av = lines
75
74
  av = av.find_all { |e| e.match?(MARK) }
76
- av = av.collect { |e| e.gsub(/\s*#{MARK}.*/, "").size }
75
+ av = av.collect { |e| e.gsub(/\s*#{MARK}.*\R/, "").size }
77
76
  av.max
78
77
  end
79
78
  end
79
+
80
+ def lang
81
+ @options[:lang] || ENV["DEFAULT_LANG"] || Source2MD.default_lang
82
+ end
80
83
  end
81
84
  end
@@ -1,10 +1,9 @@
1
1
  module Source2MD
2
2
  class Element
3
- KEY_VALUE_REGEXP = /^(?:#|\/\/)\+(\S+):\s*(.*)\R?/ # #+key: value
3
+ KEY_VALUE_REGEXP = /^\s*(?:#|\/\/)\+(\S+):\s*(.*)\R?/ # #+key: value
4
4
 
5
5
  PLUGINS = [
6
6
  Formatter::TypeHidden, # #+hidden: true
7
- Formatter::TypeMdTitle, # ## foo ##
8
7
  Formatter::TypeCodeInclude, # #+code_include: path/to/foo.html xml:SAMPLE.xml
9
8
  Formatter::TypeRawInclude, # #+raw_include: path/to/file.txt
10
9
  Formatter::TypeParseInclude, # #+parse_include: path/to/file.txt
@@ -34,13 +33,21 @@ module Source2MD
34
33
  end
35
34
 
36
35
  def body
37
- @body ||= @content.remove(KEY_VALUE_REGEXP).strip.freeze
36
+ @body ||= @content.remove(KEY_VALUE_REGEXP).freeze
38
37
  end
39
38
 
40
39
  private
41
40
 
42
41
  def support_klass
43
- @support_klass ||= PLUGINS.find { |e| e.accept?(self) }
42
+ @support_klass ||= yield_self do
43
+ Source2MD.logger.debug { "head: #{head.inspect}" }
44
+ Source2MD.logger.debug { "body: #{body.inspect}" }
45
+ PLUGINS.find do |e|
46
+ e.accept?(self).tap do |result|
47
+ Source2MD.logger.debug { "#{e} => #{result}" }
48
+ end
49
+ end
50
+ end
44
51
  end
45
52
 
46
53
  def debug_log(object)
@@ -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{(#|//)$}, "")
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{(?:#|//)\+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{(?:#|//)\+BEGIN_SRC.*?\R(.*)(?:#|//)\+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{(?:#|//)\+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{^\s*(#|//) \|.*\|$}) }
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{^\s*(#|//) })
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{^\s*(?:#|//)( |$)}
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
@@ -7,20 +7,26 @@ module Source2MD
7
7
  end
8
8
 
9
9
  def call
10
- if output_file.exist?
11
- FileUtils.chmod("a+w", output_file)
10
+ if !output_file
11
+ puts to_md
12
12
  end
13
- output_file.write(to_md)
14
- if Source2MD.readonly
15
- FileUtils.chmod("a-w", output_file)
13
+
14
+ if output_file
15
+ if output_file.exist?
16
+ FileUtils.chmod("a+w", output_file)
17
+ end
18
+ output_file.write(to_md)
19
+ if Source2MD.readonly
20
+ FileUtils.chmod("a-w", output_file)
21
+ end
22
+ puts "write: #{output_file}"
16
23
  end
17
- puts "write: #{output_file}"
18
24
  end
19
25
 
20
26
  def to_md
21
27
  s = sections.collect(&:to_md) * "\n\n"
22
28
  s = TextHelper.blank_lines_squish(s)
23
- s = TextHelper.add_newline_at_end_of_text(s)
29
+ s = TextHelper.eol_enter(s)
24
30
  end
25
31
 
26
32
  private
@@ -34,7 +40,11 @@ module Source2MD
34
40
  end
35
41
 
36
42
  def output_file
37
- @output_file ||= Pathname(params[:output_file]).expand_path
43
+ @output_file ||= yield_self do
44
+ if v = params[:output_file]
45
+ Pathname(v).expand_path
46
+ end
47
+ end
38
48
  end
39
49
  end
40
50
  end
@@ -4,7 +4,7 @@ module Source2MD
4
4
 
5
5
  SRC_BEGIN_KEY = "BEGIN_SRC"
6
6
  SRC_END_KEY = "END_SRC"
7
- SRC_BLOCK_RE = /^#\+#{SRC_BEGIN_KEY}.*?^#\+#{SRC_END_KEY}/m
7
+ SRC_BLOCK_RE = %r{^\s*(?:#|//)\+#{SRC_BEGIN_KEY}.*?^\s*(?:#|//)\+#{SRC_END_KEY}}m
8
8
 
9
9
  NORMAL_BLOCK_RE = /.*?#{SEPARATOR}/m
10
10
 
@@ -19,12 +19,12 @@ module Source2MD
19
19
 
20
20
  def to_a
21
21
  v = @content
22
+ v = v.rstrip + "\n\n"
22
23
  if Source2MD.xmp_out_exclude
23
- v = v.remove(/^# >>.*$/)
24
+ v = v.remove(%r{^(?:#|//) >>.*$})
24
25
  end
25
- v = v + "\n\n"
26
26
  v = v.scan(PARAGRAPH_RE)
27
- v = v.collect(&:strip)
27
+ v = v.collect { |e| e.rstrip + "\n" }
28
28
  v = v.find_all(&:present?)
29
29
  end
30
30
  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)
22
+ text.gsub(/^/, " ")
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.8"
2
+ VERSION = "0.0.10"
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"
data/output.md ADDED
@@ -0,0 +1,13 @@
1
+ ```ruby
2
+ a
3
+ ```
4
+
5
+ ```ruby
6
+ r.xy(); // => Vec2(0.0, 0.0)
7
+ r.xy(); // => Vec2(0.0, 0.0)
8
+ r.xy(); // => Vec2(0.0, 0.0)
9
+ ```
10
+
11
+ ```ruby
12
+ b
13
+ ```
@@ -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
@@ -17,10 +17,27 @@ module Source2MD
17
17
  #+BEGIN_SRC
18
18
  foo
19
19
  #+END_SRC
20
+
20
21
  foo
21
22
  EOS
22
23
 
23
- 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"] }
24
25
  end
25
26
  end
26
27
  end
28
+ # >> F
29
+ # >>
30
+ # >> Failures:
31
+ # >>
32
+ # >> 1) Source2MD::Scanner works
33
+ # >> Failure/Error: Unable to find - to read failed line
34
+ # >> Test::Unit::AssertionFailedError:
35
+ # >> # -:25:in `block (2 levels) in <module:Source2MD>'
36
+ # >>
37
+ # >> Finished in 0.01112 seconds (files took 0.26074 seconds to load)
38
+ # >> 1 example, 1 failure
39
+ # >>
40
+ # >> Failed examples:
41
+ # >>
42
+ # >> rspec -:5 # Source2MD::Scanner works
43
+ # >>
@@ -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.8
4
+ version: 0.0.10
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-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -165,6 +165,8 @@ files:
165
165
  - doc/0240_text_hankaku_kana.rb
166
166
  - doc/0250_parse_include.rb
167
167
  - doc/setup.rb
168
+ - examples/cli-test.rs
169
+ - examples/cli-test.sh
168
170
  - examples/cli.rb
169
171
  - examples/element_text.rb
170
172
  - examples/input.rb
@@ -175,7 +177,6 @@ files:
175
177
  - examples/type_alert.rb
176
178
  - examples/type_code_include.rb
177
179
  - examples/type_hidden.rb
178
- - examples/type_md_title.rb
179
180
  - examples/type_method.rb
180
181
  - examples/type_partial_code.rb
181
182
  - examples/type_source_block.rb
@@ -192,8 +193,8 @@ files:
192
193
  - lib/source2md/formatter/type_code_include.rb
193
194
  - lib/source2md/formatter/type_else.rb
194
195
  - lib/source2md/formatter/type_hidden.rb
195
- - lib/source2md/formatter/type_md_title.rb
196
196
  - lib/source2md/formatter/type_method.rb
197
+ - lib/source2md/formatter/type_nop.rb
197
198
  - lib/source2md/formatter/type_parse_include.rb
198
199
  - lib/source2md/formatter/type_partial_code.rb
199
200
  - lib/source2md/formatter/type_raw_include.rb
@@ -210,13 +211,13 @@ files:
210
211
  - lib/source2md/tasks/test.rake
211
212
  - lib/source2md/text_helper.rb
212
213
  - lib/source2md/version.rb
214
+ - output.md
213
215
  - source2md.gemspec
214
216
  - spec/code_block_spec.rb
215
217
  - spec/formatter/sample.yml
216
218
  - spec/formatter/type_alert_spec.rb
217
219
  - spec/formatter/type_code_include_spec.rb
218
220
  - spec/formatter/type_hidden_spec.rb
219
- - spec/formatter/type_md_title_spec.rb
220
221
  - spec/formatter/type_method_spec.rb
221
222
  - spec/formatter/type_parse_include_spec.rb
222
223
  - spec/formatter/type_partial_code_spec.rb
@@ -258,7 +259,6 @@ test_files:
258
259
  - spec/formatter/type_alert_spec.rb
259
260
  - spec/formatter/type_code_include_spec.rb
260
261
  - spec/formatter/type_hidden_spec.rb
261
- - spec/formatter/type_md_title_spec.rb
262
262
  - spec/formatter/type_method_spec.rb
263
263
  - spec/formatter/type_parse_include_spec.rb
264
264
  - spec/formatter/type_partial_code_spec.rb
@@ -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