source2md 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +112 -29
- data/Rakefile +1 -0
- data/doc/0100_top.rb +7 -0
- data/doc/0110_code_small.rb +4 -1
- data/doc/0120_code_long.rb +2 -1
- data/doc/0130_hidden.rb +17 -0
- data/doc/0140_code_include.rb +26 -0
- data/doc/0150_raw_include.rb +16 -0
- data/doc/{0130_title.rb → 0160_title.rb} +8 -5
- data/doc/{0140_md_like_title.rb → 0170_md_like_title.rb} +9 -6
- data/doc/{0150_table.rb → 0180_table.rb} +2 -1
- data/doc/{0160_method.rb → 0190_method.rb} +2 -1
- data/doc/{0170_alert.rb → 0200_alert.rb} +2 -1
- data/doc/0210_text.rb +18 -0
- data/doc/0220_text_oneline.rb +18 -0
- data/doc/0230_parse_include.rb +18 -0
- data/examples/cli.rb +1 -0
- data/examples/element_text.rb +2 -1
- data/examples/scanner.rb +1 -0
- data/examples/{element_alert.rb → type_alert.rb} +6 -5
- data/examples/type_code_include.rb +21 -0
- data/examples/{element_md_header.rb → type_hidden.rb} +2 -1
- data/examples/{element_md_title.rb → type_md_title.rb} +2 -1
- data/examples/{element_method.rb → type_method.rb} +2 -1
- data/examples/{element_partial_code.rb → type_partial_code.rb} +2 -1
- data/examples/{element_source_block.rb → type_source_block.rb} +2 -1
- data/examples/{element_table.rb → type_table.rb} +1 -0
- data/examples/{element_deep_comment.rb → type_text.rb} +16 -10
- data/examples/{element_title.rb → type_title.rb} +2 -1
- data/examples/{element_warn.rb → type_warn.rb} +2 -1
- data/lib/source2md/code_block.rb +12 -6
- data/lib/source2md/element.rb +17 -16
- data/lib/source2md/{type → formatter}/base.rb +4 -5
- data/lib/source2md/{type/element_alert.rb → formatter/type_alert.rb} +4 -4
- data/lib/source2md/{type/element_include.rb → formatter/type_code_include.rb} +5 -5
- data/lib/source2md/{type/element_else.rb → formatter/type_else.rb} +2 -2
- data/lib/source2md/{type/element_reject.rb → formatter/type_hidden.rb} +4 -3
- data/lib/source2md/{type/element_md_title.rb → formatter/type_md_title.rb} +2 -2
- data/lib/source2md/formatter/type_method.rb +24 -0
- data/lib/source2md/formatter/type_parse_include.rb +13 -0
- data/lib/source2md/{type/element_partial_code.rb → formatter/type_partial_code.rb} +2 -2
- data/lib/source2md/formatter/type_raw_include.rb +13 -0
- data/lib/source2md/{type/element_source_block.rb → formatter/type_source_block.rb} +2 -2
- data/lib/source2md/{type/element_table.rb → formatter/type_table.rb} +2 -2
- data/lib/source2md/formatter/type_text.rb +31 -0
- data/lib/source2md/{type/element_title.rb → formatter/type_title.rb} +2 -2
- data/lib/source2md/{type/element_warn.rb → formatter/type_warn.rb} +4 -4
- data/lib/source2md/generator.rb +3 -3
- data/lib/source2md/scanner.rb +2 -2
- data/lib/source2md/text_helper.rb +5 -1
- data/lib/source2md/version.rb +1 -1
- data/spec/code_block_spec.rb +1 -1
- data/spec/formatter/sample.yml +1 -0
- data/spec/{type/element_include_spec.rb → formatter/type_code_include_spec.rb} +6 -6
- data/spec/{type/element_reject_spec.rb → formatter/type_hidden_spec.rb} +3 -2
- data/spec/formatter/type_parse_include_spec.rb +16 -0
- data/spec/{type/element_md_header_spec.rb → formatter/type_raw_include_spec.rb} +2 -6
- data/spec/{type/element_text_spec.rb → formatter/type_text_spec.rb} +14 -2
- metadata +67 -62
- data/doc/0180_text.rb +0 -15
- data/doc/0190_raw.rb +0 -19
- data/examples/element_include.rb +0 -20
- data/examples/element_reject.rb +0 -17
- data/lib/source2md/type/element_deep_comment.rb +0 -19
- data/lib/source2md/type/element_md_header.rb +0 -13
- data/lib/source2md/type/element_method.rb +0 -24
- data/lib/source2md/type/element_uncomment.rb +0 -24
- data/spec/type/sample.yml +0 -1
- /data/spec/{type/element_alert_spec.rb → formatter/type_alert_spec.rb} +0 -0
- /data/spec/{type/element_md_title_spec.rb → formatter/type_md_title_spec.rb} +0 -0
- /data/spec/{type/element_method_spec.rb → formatter/type_method_spec.rb} +0 -0
- /data/spec/{type/element_partial_code_spec.rb → formatter/type_partial_code_spec.rb} +0 -0
- /data/spec/{type/element_source_block_spec.rb → formatter/type_source_block_spec.rb} +0 -0
- /data/spec/{type/element_table_spec.rb → formatter/type_table_spec.rb} +0 -0
- /data/spec/{type/element_title_spec.rb → formatter/type_title_spec.rb} +0 -0
- /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::
|
8
|
+
# >> Source2MD::Part::ElementInfo
|
8
9
|
# >>
|
9
10
|
# >> head:
|
10
11
|
# >> {:info=>"(foo)"}
|
data/lib/source2md/code_block.rb
CHANGED
@@ -8,7 +8,8 @@ module Source2MD
|
|
8
8
|
def initialize(code, options = {})
|
9
9
|
@code = code
|
10
10
|
@options = {
|
11
|
-
lang
|
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
|
-
|
47
|
-
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
|
-
|
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}(.*)/) {
|
data/lib/source2md/element.rb
CHANGED
@@ -1,21 +1,22 @@
|
|
1
1
|
module Source2MD
|
2
2
|
class Element
|
3
|
-
|
3
|
+
KEY_VALUE_REGEXP = /^#\+(\S+):\s*(.*)\R?/ # #+key: value
|
4
4
|
|
5
5
|
PLUGINS = [
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
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(
|
33
|
+
@head ||= @content.scan(KEY_VALUE_REGEXP).to_h.freeze
|
33
34
|
end
|
34
35
|
|
35
36
|
def body
|
36
|
-
@body ||= @content.remove(
|
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
|
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
|
-
|
15
|
-
|
16
|
-
|
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
|
3
|
-
class
|
2
|
+
module Formatter
|
3
|
+
class TypeAlert < Base
|
4
4
|
def self.accept?(element)
|
5
|
-
element.head[
|
5
|
+
element.head["alert"]
|
6
6
|
end
|
7
7
|
|
8
8
|
def to_md
|
9
9
|
[
|
10
10
|
":::message alert",
|
11
|
-
element.head[
|
11
|
+
element.head["alert"],
|
12
12
|
":::",
|
13
13
|
] * "\n"
|
14
14
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module Source2MD
|
2
|
-
module
|
3
|
-
class
|
2
|
+
module Formatter
|
3
|
+
class TypeCodeInclude < Base
|
4
4
|
def self.accept?(element)
|
5
|
-
element.head[
|
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[
|
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,11 +1,12 @@
|
|
1
1
|
module Source2MD
|
2
|
-
module
|
3
|
-
class
|
2
|
+
module Formatter
|
3
|
+
class TypeHidden < Base
|
4
4
|
def self.accept?(element)
|
5
|
-
element.
|
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
|
@@ -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,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,16 +1,16 @@
|
|
1
1
|
module Source2MD
|
2
|
-
module
|
3
|
-
class
|
2
|
+
module Formatter
|
3
|
+
class TypeWarn < Base
|
4
4
|
def self.accept?(element)
|
5
5
|
if element.body.empty?
|
6
|
-
element.head[
|
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[
|
13
|
+
element.head["warn"],
|
14
14
|
":::",
|
15
15
|
] * "\n"
|
16
16
|
end
|
data/lib/source2md/generator.rb
CHANGED
@@ -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
|
data/lib/source2md/scanner.rb
CHANGED
@@ -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{
|
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
|
data/lib/source2md/version.rb
CHANGED
data/spec/code_block_spec.rb
CHANGED
@@ -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
|
-
#+
|
7
|
+
#+code_include: #{__dir__}/sample.yml
|
8
8
|
EOS
|
9
9
|
actual.should == <<~EOS.strip
|
10
10
|
```yml:sample.yml
|
11
|
-
yaml
|
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
|
-
#+
|
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
|
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
|
-
#+
|
31
|
+
#+code_include: #{__dir__}/sample.yml yaml:filename
|
32
32
|
EOS
|
33
33
|
actual.should == <<~EOS.strip
|
34
34
|
```yaml:filename
|
35
|
-
yaml
|
35
|
+
(yaml)
|
36
36
|
```
|
37
37
|
EOS
|
38
38
|
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
|
-
|
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
|