source2md 0.0.8 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/examples/cli-test.rs +6 -0
- data/examples/cli-test.sh +2 -0
- data/examples/type_alert.rb +10 -14
- data/examples/type_partial_code.rb +20 -16
- data/examples/type_text.rb +6 -6
- data/examples/type_title.rb +11 -19
- data/examples/type_warn.rb +11 -15
- data/lib/source2md/cli.rb +4 -4
- data/lib/source2md/code_block.rb +20 -17
- data/lib/source2md/element.rb +11 -4
- data/lib/source2md/formatter/type_code_include.rb +1 -1
- data/lib/source2md/formatter/type_method.rb +15 -7
- data/lib/source2md/formatter/{type_md_title.rb → type_nop.rb} +3 -3
- data/lib/source2md/formatter/type_raw_include.rb +2 -1
- data/lib/source2md/formatter/type_source_block.rb +4 -4
- data/lib/source2md/formatter/type_table.rb +5 -5
- data/lib/source2md/formatter/type_text.rb +5 -3
- data/lib/source2md/generator.rb +18 -8
- data/lib/source2md/scanner.rb +4 -4
- data/lib/source2md/text_helper.rb +13 -4
- data/lib/source2md/version.rb +1 -1
- data/lib/source2md.rb +1 -0
- data/output.md +13 -0
- data/spec/code_block_spec.rb +2 -1
- data/spec/formatter/type_code_include_spec.rb +3 -3
- data/spec/formatter/type_method_spec.rb +3 -2
- data/spec/formatter/type_partial_code_spec.rb +1 -1
- data/spec/formatter/type_raw_include_spec.rb +1 -1
- data/spec/formatter/type_source_block_spec.rb +2 -2
- data/spec/formatter/type_table_spec.rb +2 -2
- data/spec/formatter/type_text_spec.rb +5 -4
- data/spec/scanner_spec.rb +18 -1
- data/spec/text_helper_spec.rb +8 -4
- metadata +6 -6
- data/examples/type_md_title.rb +0 -15
- 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2ac00b7cc8674c792566d8d08c5d2f1b8f5cf755ceca1479b2ac8248ad6dac3e
|
4
|
+
data.tar.gz: e7c61a968ca24419cc5a62c86437388f77cd865895ac038a5565b1a4ab4c180d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 25222fde61442a0fceba5daa0e0ff09e7343c0fcacf7123670d85ee66f3a4df50533ec40b211b851eea0870bf64f781b9dd283dc26650a1d7a004d27a95dd513
|
7
|
+
data.tar.gz: c89ccfdb613ee1a8fa95eef6a1524be3fe7534a2e5c2fd18fceba4895f87711febc078292f4b974c1ae7a525d75af8e2e528c10b59a55678c7f2730814776077
|
data/examples/type_alert.rb
CHANGED
@@ -4,20 +4,16 @@ require "./setup"
|
|
4
4
|
puts Source2MD::Element.new(<<~EOS).to_md
|
5
5
|
#+alert: (foo)
|
6
6
|
EOS
|
7
|
-
# >> --------------------------------------------------------------------------------
|
8
|
-
# >>
|
9
|
-
# >>
|
10
|
-
# >>
|
11
|
-
# >>
|
12
|
-
# >>
|
13
|
-
# >>
|
14
|
-
# >>
|
15
|
-
# >>
|
16
|
-
# >>
|
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
|
-
|
5
|
+
a
|
6
|
+
b
|
7
|
+
c
|
6
8
|
EOS
|
7
|
-
# >> --------------------------------------------------------------------------------
|
8
|
-
# >>
|
9
|
-
# >>
|
10
|
-
# >>
|
11
|
-
# >>
|
12
|
-
# >>
|
13
|
-
# >>
|
14
|
-
# >>
|
15
|
-
# >>
|
16
|
-
# >>
|
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
|
-
# >>
|
19
|
-
# >>
|
20
|
-
# >>
|
21
|
-
# >> ```ruby
|
22
|
-
# >> (foo)
|
24
|
+
# >> a
|
25
|
+
# >> b
|
26
|
+
# >> c
|
23
27
|
# >> ```
|
data/examples/type_text.rb
CHANGED
@@ -25,18 +25,18 @@ puts Source2MD::Element.new(<<~EOS).to_md
|
|
25
25
|
EOS
|
26
26
|
|
27
27
|
# >> > -------------------------------------------------------------------------------- Source2MD::Formatter::TypeText
|
28
|
-
# >> > {"
|
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
|
-
# >> >
|
37
|
+
# >> > a b c d
|
38
38
|
# >> > ------------------------------------------------------------
|
39
|
-
# >>
|
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
|
# >> > ------------------------------------------------------------
|
data/examples/type_title.rb
CHANGED
@@ -4,22 +4,14 @@ require "./setup"
|
|
4
4
|
puts Source2MD::Element.new(<<~EOS).to_md
|
5
5
|
#+title2: (foo)
|
6
6
|
EOS
|
7
|
-
# >>
|
8
|
-
# >> --------------------------------------------------------------------------------
|
9
|
-
# >>
|
10
|
-
# >>
|
11
|
-
# >>
|
12
|
-
# >>
|
13
|
-
# >>
|
14
|
-
# >>
|
15
|
-
# >>
|
16
|
-
# >>
|
17
|
-
# >>
|
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) ##
|
data/examples/type_warn.rb
CHANGED
@@ -2,22 +2,18 @@
|
|
2
2
|
require "./setup"
|
3
3
|
|
4
4
|
puts Source2MD::Element.new(<<~EOS).to_md
|
5
|
-
#+
|
5
|
+
#+warn: (foo)
|
6
6
|
EOS
|
7
|
-
# >> --------------------------------------------------------------------------------
|
8
|
-
# >>
|
9
|
-
# >>
|
10
|
-
# >>
|
11
|
-
# >>
|
12
|
-
# >>
|
13
|
-
# >>
|
14
|
-
# >>
|
15
|
-
# >>
|
16
|
-
# >>
|
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,
|
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:
|
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
|
data/lib/source2md/code_block.rb
CHANGED
@@ -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
|
-
|
7
|
-
|
8
|
-
def initialize(code, options = {})
|
9
|
-
@code = code
|
6
|
+
def initialize(text, options = {})
|
7
|
+
@text = text
|
10
8
|
@options = {
|
11
|
-
:lang =>
|
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
|
-
]
|
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 =
|
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))
|
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
|
-
|
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 ||=
|
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}
|
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
|
data/lib/source2md/element.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
module Source2MD
|
2
2
|
class Element
|
3
|
-
KEY_VALUE_REGEXP =
|
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).
|
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 ||=
|
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)
|
@@ -6,18 +6,26 @@ module Source2MD
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def to_md
|
9
|
-
[
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
3
|
+
class TypeNop < Base
|
4
4
|
def self.accept?(element)
|
5
|
-
element.head
|
5
|
+
element.head["nop"]
|
6
6
|
end
|
7
7
|
|
8
8
|
def to_md
|
9
|
-
|
9
|
+
""
|
10
10
|
end
|
11
11
|
end
|
12
12
|
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.
|
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(
|
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(
|
23
|
-
md.captures.first
|
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(/-\+-/, "-|-")
|
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.
|
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
|
data/lib/source2md/generator.rb
CHANGED
@@ -7,20 +7,26 @@ module Source2MD
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def call
|
10
|
-
if output_file
|
11
|
-
|
10
|
+
if !output_file
|
11
|
+
puts to_md
|
12
12
|
end
|
13
|
-
|
14
|
-
if
|
15
|
-
|
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.
|
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 ||=
|
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
|
data/lib/source2md/scanner.rb
CHANGED
@@ -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 =
|
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
|
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
|
data/lib/source2md/version.rb
CHANGED
data/lib/source2md.rb
CHANGED
data/output.md
ADDED
data/spec/code_block_spec.rb
CHANGED
@@ -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
|
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
|
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
|
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
|
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
|
@@ -8,7 +8,7 @@ module Source2MD
|
|
8
8
|
(foo)
|
9
9
|
#+END_SRC
|
10
10
|
EOS
|
11
|
-
actual.should == <<~EOS
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
+
# >>
|
data/spec/text_helper_spec.rb
CHANGED
@@ -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 "
|
10
|
-
assert { TextHelper.
|
11
|
-
assert { TextHelper.
|
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.
|
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-
|
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
|
data/examples/type_md_title.rb
DELETED
@@ -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 ##
|