source2md 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +196 -40
- data/Rakefile +26 -0
- data/doc/0100_top.rb +17 -0
- data/doc/0110_code_small.rb +23 -0
- data/doc/0120_code_long.rb +23 -0
- data/doc/0130_title.rb +21 -0
- data/doc/0140_md_like_title.rb +21 -0
- data/doc/0150_table.rb +35 -0
- data/doc/0160_method.rb +37 -0
- data/doc/0170_alert.rb +25 -0
- data/doc/0180_text.rb +15 -0
- data/doc/0190_raw.rb +19 -0
- data/doc/setup.rb +4 -0
- data/examples/element_alert.rb +22 -0
- data/examples/element_deep_comment.rb +12 -16
- data/examples/element_include.rb +20 -0
- data/examples/element_md_header.rb +4 -4
- data/examples/element_md_title.rb +14 -0
- data/examples/element_method.rb +3 -3
- data/examples/{element_pre.rb → element_partial_code.rb} +5 -5
- data/examples/element_source_block.rb +28 -0
- data/examples/element_table.rb +17 -25
- data/examples/element_text.rb +1 -1
- data/examples/{element_title2.rb → element_title.rb} +12 -8
- data/examples/element_warn.rb +22 -0
- data/examples/input.rb +11 -7
- data/examples/input.yml +3 -0
- data/examples/output.md +7 -22
- data/examples/scanner.rb +24 -0
- data/examples/setup.rb +1 -1
- data/lib/source2md/cli.rb +8 -1
- data/lib/source2md/code_block.rb +21 -2
- data/lib/source2md/element.rb +36 -13
- data/lib/source2md/generator.rb +4 -4
- data/lib/source2md/scanner.rb +34 -0
- data/lib/source2md/{file_block.rb → section.rb} +4 -5
- data/lib/source2md/text_helper.rb +1 -1
- data/lib/source2md/type/base.rb +4 -19
- data/lib/source2md/type/element_alert.rb +17 -0
- data/lib/source2md/type/element_else.rb +18 -0
- data/lib/source2md/type/element_include.rb +45 -0
- data/lib/source2md/type/element_md_title.rb +13 -0
- data/lib/source2md/type/element_method.rb +1 -1
- data/lib/source2md/type/{element_pre.rb → element_partial_code.rb} +2 -2
- data/lib/source2md/type/element_source_block.rb +25 -0
- data/lib/source2md/type/element_table.rb +4 -4
- data/lib/source2md/type/element_title.rb +35 -0
- data/lib/source2md/type/element_uncomment.rb +24 -0
- data/lib/source2md/type/element_warn.rb +19 -0
- data/lib/source2md/version.rb +1 -1
- data/lib/source2md.rb +2 -0
- data/source2md.gemspec +4 -3
- data/spec/code_block_spec.rb +1 -1
- data/spec/scanner_spec.rb +26 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/text_helper_spec.rb +15 -0
- data/spec/type/element_alert_spec.rb +16 -0
- data/spec/type/element_include_spec.rb +40 -0
- data/spec/type/element_md_header_spec.rb +1 -17
- data/spec/type/{element_title2_spec.rb → element_md_title_spec.rb} +3 -3
- data/spec/type/element_method_spec.rb +5 -5
- data/spec/type/{element_pre_spec.rb → element_partial_code_spec.rb} +3 -3
- data/spec/type/element_reject_spec.rb +1 -1
- data/spec/type/element_source_block_spec.rb +31 -0
- data/spec/type/element_table_spec.rb +14 -1
- data/spec/type/element_text_spec.rb +1 -1
- data/spec/type/{element_deep_comment_spec.rb → element_title_spec.rb} +3 -5
- data/spec/type/element_warn_spec.rb +16 -0
- data/spec/type/sample.yml +1 -0
- metadata +70 -17
- data/lib/source2md/type/element_text.rb +0 -26
- data/lib/source2md/type/element_title2.rb +0 -14
data/examples/element_method.rb
CHANGED
@@ -1,22 +1,22 @@
|
|
1
1
|
require "./setup"
|
2
2
|
|
3
3
|
puts Source2MD::Element.new(<<~EOS).to_md
|
4
|
-
|
4
|
+
(foo)
|
5
5
|
EOS
|
6
6
|
# >> --------------------------------------------------------------------------------
|
7
|
-
# >> Source2MD::Type::
|
7
|
+
# >> Source2MD::Type::ElementPartialCode
|
8
8
|
# >>
|
9
9
|
# >> head:
|
10
10
|
# >> {}
|
11
11
|
# >>
|
12
12
|
# >> in:
|
13
|
-
# >>
|
13
|
+
# >> (foo)
|
14
14
|
# >>
|
15
15
|
# >> out:
|
16
16
|
# >> ```ruby
|
17
|
-
# >>
|
17
|
+
# >> (foo)
|
18
18
|
# >> ```
|
19
19
|
# >> --------------------------------------------------------------------------------
|
20
20
|
# >> ```ruby
|
21
|
-
# >>
|
21
|
+
# >> (foo)
|
22
22
|
# >> ```
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require "./setup"
|
2
|
+
|
3
|
+
puts Source2MD::Element.new(<<~EOS).to_md
|
4
|
+
#+BEGIN_SRC diff xxx:yyy
|
5
|
+
(foo)
|
6
|
+
#+END_SRC
|
7
|
+
EOS
|
8
|
+
# >> --------------------------------------------------------------------------------
|
9
|
+
# >> Source2MD::Type::ElementSourceBlock
|
10
|
+
# >>
|
11
|
+
# >> head:
|
12
|
+
# >> {}
|
13
|
+
# >>
|
14
|
+
# >> in:
|
15
|
+
# >> #+BEGIN_SRC diff xxx:yyy
|
16
|
+
# >> (foo)
|
17
|
+
# >> #+END_SRC
|
18
|
+
# >>
|
19
|
+
# >> out:
|
20
|
+
# >> ```diff xxx:yyy
|
21
|
+
# >> diff xxx:yyy
|
22
|
+
# >> (foo)
|
23
|
+
# >> ```
|
24
|
+
# >> --------------------------------------------------------------------------------
|
25
|
+
# >> ```diff xxx:yyy
|
26
|
+
# >> diff xxx:yyy
|
27
|
+
# >> (foo)
|
28
|
+
# >> ```
|
data/examples/element_table.rb
CHANGED
@@ -2,33 +2,25 @@ require "./setup"
|
|
2
2
|
|
3
3
|
puts Source2MD::Element.new(<<~EOS).to_md
|
4
4
|
# |---+---+---|
|
5
|
-
# |
|
5
|
+
# | 1 | 1 | 1 |
|
6
6
|
# |---+---+---|
|
7
|
-
# |
|
8
|
-
# |
|
7
|
+
# | 1 | 1 | 1 |
|
8
|
+
# | 1 | 1 | 1 |
|
9
9
|
# |---+---+---|
|
10
10
|
EOS
|
11
|
-
|
12
|
-
|
13
|
-
#
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
17
|
-
|
18
|
-
|
19
|
-
# >>
|
20
|
-
# >> # |---+---+---|
|
21
|
-
# >> # | x | x | x |
|
22
|
-
# >> # | x | x | x |
|
23
|
-
# >> # |---+---+---|
|
24
|
-
# >>
|
25
|
-
# >> out:
|
26
|
-
# >> | x | x | x |
|
11
|
+
|
12
|
+
puts Source2MD::Element.new(<<~EOS).to_md
|
13
|
+
# | 2 | 2 | 2 |
|
14
|
+
# |---+---+---|
|
15
|
+
# | 2 | 2 | 2 |
|
16
|
+
# | 2 | 2 | 2 |
|
17
|
+
EOS
|
18
|
+
|
19
|
+
# >> | 1 | 1 | 1 |
|
27
20
|
# >> |---|---|---|
|
28
|
-
# >> |
|
29
|
-
# >> |
|
30
|
-
# >>
|
31
|
-
# >> | x | x | x |
|
21
|
+
# >> | 1 | 1 | 1 |
|
22
|
+
# >> | 1 | 1 | 1 |
|
23
|
+
# >> | 2 | 2 | 2 |
|
32
24
|
# >> |---|---|---|
|
33
|
-
# >> |
|
34
|
-
# >> |
|
25
|
+
# >> | 2 | 2 | 2 |
|
26
|
+
# >> | 2 | 2 | 2 |
|
data/examples/element_text.rb
CHANGED
@@ -5,7 +5,7 @@ puts Source2MD::Element.new(<<~EOS).to_md
|
|
5
5
|
# https://example.com/
|
6
6
|
EOS
|
7
7
|
# >> --------------------------------------------------------------------------------
|
8
|
-
# >> Source2MD::Type::
|
8
|
+
# >> Source2MD::Type::ElementUncomment
|
9
9
|
# >>
|
10
10
|
# >> head:
|
11
11
|
# >> {}
|
@@ -1,20 +1,24 @@
|
|
1
1
|
require "./setup"
|
2
2
|
|
3
3
|
puts Source2MD::Element.new(<<~EOS).to_md
|
4
|
-
|
4
|
+
#+title2: (foo)
|
5
5
|
EOS
|
6
6
|
# >> title2: (foo)
|
7
7
|
# >> --------------------------------------------------------------------------------
|
8
|
-
# >> Source2MD::Type::
|
9
|
-
# >>
|
8
|
+
# >> Source2MD::Type::ElementTitle
|
9
|
+
# >>
|
10
10
|
# >> head:
|
11
11
|
# >> {:title2=>"(foo)"}
|
12
|
-
# >>
|
12
|
+
# >>
|
13
13
|
# >> in:
|
14
|
-
# >>
|
15
|
-
# >>
|
14
|
+
# >>
|
15
|
+
# >>
|
16
16
|
# >> out:
|
17
|
-
# >>
|
17
|
+
# >>
|
18
|
+
# >> (foo)
|
19
|
+
# >>
|
18
20
|
# >> --------------------------------------------------------------------------------
|
19
21
|
# >> title2: (foo)
|
20
|
-
# >>
|
22
|
+
# >>
|
23
|
+
# >> (foo)
|
24
|
+
# >>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require "./setup"
|
2
|
+
|
3
|
+
puts Source2MD::Element.new(<<~EOS).to_md
|
4
|
+
#+info: (foo)
|
5
|
+
EOS
|
6
|
+
# >> --------------------------------------------------------------------------------
|
7
|
+
# >> Source2MD::Type::ElementInfo
|
8
|
+
# >>
|
9
|
+
# >> head:
|
10
|
+
# >> {:info=>"(foo)"}
|
11
|
+
# >>
|
12
|
+
# >> in:
|
13
|
+
# >>
|
14
|
+
# >>
|
15
|
+
# >> out:
|
16
|
+
# >> :::message
|
17
|
+
# >> (foo)
|
18
|
+
# >> :::
|
19
|
+
# >> --------------------------------------------------------------------------------
|
20
|
+
# >> :::message
|
21
|
+
# >> (foo)
|
22
|
+
# >> :::
|
data/examples/input.rb
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
|
1
|
+
#+title2: (title)
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
#+name: (name1)
|
4
|
+
#+desc: (desc1)
|
5
|
+
#+comment: (comment1)
|
6
6
|
"foo1".size # => 4
|
7
7
|
|
8
|
-
|
9
|
-
|
10
|
-
|
8
|
+
#+name: (name2)
|
9
|
+
#+desc: (desc2)
|
10
|
+
#+comment: (comment2)
|
11
11
|
"foo2".size # => 4
|
12
12
|
|
13
13
|
# |---+---+---|
|
@@ -16,3 +16,7 @@
|
|
16
16
|
# | x | x | x |
|
17
17
|
# | x | x | x |
|
18
18
|
# |---+---+---|
|
19
|
+
|
20
|
+
#+BEGIN_SRC
|
21
|
+
1 + 2 # => 3
|
22
|
+
#+END_SRC
|
data/examples/input.yml
ADDED
data/examples/output.md
CHANGED
@@ -1,26 +1,11 @@
|
|
1
|
-
## (title) ##
|
2
|
-
|
3
|
-
### (name1)
|
4
|
-
|
5
|
-
(desc1)
|
6
|
-
|
7
1
|
```ruby
|
8
|
-
|
2
|
+
# ```yaml:config.yml
|
3
|
+
# frequencies: frequencies1.rb
|
4
|
+
# ```
|
9
5
|
```
|
10
6
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
(desc2)
|
16
|
-
|
17
|
-
```ruby
|
18
|
-
"foo2".size # => 4
|
7
|
+
```ruby:frequencies1.rb
|
8
|
+
def frequencies(words)
|
9
|
+
words.tally.sort_by { -_2 }.take(25)
|
10
|
+
end
|
19
11
|
```
|
20
|
-
|
21
|
-
(comment2)
|
22
|
-
|
23
|
-
| x | x | x |
|
24
|
-
|---|---|---|
|
25
|
-
| x | x | x |
|
26
|
-
| x | x | x |
|
data/examples/scanner.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require "./setup"
|
2
|
+
|
3
|
+
Source2MD::Scanner.new(<<~EOS).to_a # => ["foo", "#+BEGIN_SRC\n\nfoo\n\n#+END_SRC", "foo"]
|
4
|
+
foo
|
5
|
+
|
6
|
+
#+BEGIN_SRC
|
7
|
+
|
8
|
+
foo
|
9
|
+
|
10
|
+
#+END_SRC
|
11
|
+
|
12
|
+
foo
|
13
|
+
EOS
|
14
|
+
|
15
|
+
Source2MD::Scanner.new(<<~EOS).to_a # => ["foo", "# ```yaml\n#+foo:\n# a\n# ```", "bar"]
|
16
|
+
foo
|
17
|
+
|
18
|
+
# ```yaml
|
19
|
+
#+foo:
|
20
|
+
# a
|
21
|
+
# ```
|
22
|
+
|
23
|
+
bar
|
24
|
+
EOS
|
data/examples/setup.rb
CHANGED
data/lib/source2md/cli.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module Source2MD
|
2
2
|
class Cli < Thor
|
3
|
-
class_option :debug,
|
3
|
+
class_option :debug, type: :boolean, aliases: "-d", default: false
|
4
|
+
class_option :xmp_out_exclude, type: :boolean, aliases: "-x", default: false
|
4
5
|
|
5
6
|
def initialize(...)
|
6
7
|
super
|
@@ -9,8 +10,14 @@ module Source2MD
|
|
9
10
|
Source2MD.logger.level = :debug
|
10
11
|
Source2MD.debug = true
|
11
12
|
end
|
13
|
+
|
14
|
+
Source2MD.xmp_out_exclude = options[:xmp_out_exclude]
|
15
|
+
|
16
|
+
tp Source2MD.config
|
12
17
|
end
|
13
18
|
|
19
|
+
# default_command :generate
|
20
|
+
|
14
21
|
map "g" => :generate
|
15
22
|
desc "generate [files]", "Markdown generation"
|
16
23
|
option :output_file, type: :string, aliases: "-o", default: "output.md"
|
data/lib/source2md/code_block.rb
CHANGED
@@ -5,13 +5,16 @@ module Source2MD
|
|
5
5
|
|
6
6
|
attr_accessor :code
|
7
7
|
|
8
|
-
def initialize(code)
|
8
|
+
def initialize(code, options = {})
|
9
9
|
@code = code
|
10
|
+
@options = {
|
11
|
+
lang: "ruby",
|
12
|
+
}.merge(options)
|
10
13
|
end
|
11
14
|
|
12
15
|
def to_md
|
13
16
|
[
|
14
|
-
"
|
17
|
+
"```#{code_block_head}",
|
15
18
|
normalized_code,
|
16
19
|
"```",
|
17
20
|
] * "\n"
|
@@ -19,6 +22,22 @@ module Source2MD
|
|
19
22
|
|
20
23
|
private
|
21
24
|
|
25
|
+
def code_block_head
|
26
|
+
o = []
|
27
|
+
if s = @options[:desc]
|
28
|
+
o << s
|
29
|
+
else
|
30
|
+
if s = @options[:lang]
|
31
|
+
o << s
|
32
|
+
end
|
33
|
+
if s = @options[:name]
|
34
|
+
o << ":"
|
35
|
+
o << s
|
36
|
+
end
|
37
|
+
end
|
38
|
+
o.join
|
39
|
+
end
|
40
|
+
|
22
41
|
def normalized_code
|
23
42
|
lines.collect(&method(:normalize)) * "\n"
|
24
43
|
end
|
data/lib/source2md/element.rb
CHANGED
@@ -1,16 +1,21 @@
|
|
1
1
|
module Source2MD
|
2
2
|
class Element
|
3
|
-
KEY_VALUE =
|
3
|
+
KEY_VALUE = /^#\+(\S+):\s*(.*)\R?/
|
4
4
|
|
5
5
|
PLUGINS = [
|
6
|
-
Type::ElementReject,
|
7
|
-
Type::ElementMdHeader,
|
8
|
-
Type::
|
9
|
-
Type::
|
10
|
-
Type::
|
11
|
-
Type::
|
12
|
-
Type::
|
13
|
-
Type::
|
6
|
+
Type::ElementReject, # require "setup"
|
7
|
+
Type::ElementMdHeader, # ---
|
8
|
+
Type::ElementMdTitle, # ## foo ##
|
9
|
+
Type::ElementInclude, # #+include: foo.txt
|
10
|
+
Type::ElementTitle, # #+title2: foo
|
11
|
+
Type::ElementWarn, # #+warn: foo
|
12
|
+
Type::ElementAlert, # #+alert: foo
|
13
|
+
Type::ElementMethod, # #+name: foo
|
14
|
+
Type::ElementTable, # |-
|
15
|
+
Type::ElementSourceBlock, # #+BEGIN_SRC
|
16
|
+
Type::ElementUncomment, # foo
|
17
|
+
Type::ElementPartialCode, # 1 + 2 # => 3
|
18
|
+
Type::ElementElse,
|
14
19
|
]
|
15
20
|
|
16
21
|
def initialize(content)
|
@@ -18,21 +23,39 @@ module Source2MD
|
|
18
23
|
end
|
19
24
|
|
20
25
|
def to_md
|
21
|
-
support_klass.new(self)
|
26
|
+
object = support_klass.new(self)
|
27
|
+
debug_log(object)
|
28
|
+
object.to_md
|
22
29
|
end
|
23
30
|
|
24
31
|
def head
|
25
|
-
@content.scan(KEY_VALUE).to_h.symbolize_keys
|
32
|
+
@head ||= @content.scan(KEY_VALUE).to_h.symbolize_keys.freeze
|
26
33
|
end
|
27
34
|
|
28
35
|
def body
|
29
|
-
@content.remove(KEY_VALUE).strip
|
36
|
+
@body ||= @content.remove(KEY_VALUE).strip.freeze
|
30
37
|
end
|
31
38
|
|
32
39
|
private
|
33
40
|
|
34
41
|
def support_klass
|
35
|
-
PLUGINS.find { |e| e.accept?(self) }
|
42
|
+
@support_klass ||= PLUGINS.find { |e| e.accept?(self) }
|
43
|
+
end
|
44
|
+
|
45
|
+
def debug_log(object)
|
46
|
+
Source2MD.logger.debug do
|
47
|
+
o = []
|
48
|
+
o << "-" * 80 + " " + object.class.name
|
49
|
+
o << head
|
50
|
+
o << "-" * 60 + " " + "in"
|
51
|
+
o << body
|
52
|
+
o << ""
|
53
|
+
o << "-" * 60 + " " + "out"
|
54
|
+
o << object.to_md
|
55
|
+
o << "-" * 60
|
56
|
+
s = o.compact * "\n"
|
57
|
+
s.gsub(/^/, "> ")
|
58
|
+
end
|
36
59
|
end
|
37
60
|
end
|
38
61
|
end
|
data/lib/source2md/generator.rb
CHANGED
@@ -14,13 +14,13 @@ module Source2MD
|
|
14
14
|
private
|
15
15
|
|
16
16
|
def to_md
|
17
|
-
s =
|
18
|
-
s = TextHelper.
|
17
|
+
s = sections.collect(&:to_md) * "\n\n"
|
18
|
+
s = TextHelper.blank_lines_squish(s)
|
19
19
|
s = TextHelper.add_newline_at_end_of_text(s)
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
@
|
22
|
+
def sections
|
23
|
+
@sections ||= files.collect { |e| Section.new(e.read) }
|
24
24
|
end
|
25
25
|
|
26
26
|
def files
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Source2MD
|
2
|
+
class Scanner
|
3
|
+
SEPARATOR = "\\R{2,}"
|
4
|
+
|
5
|
+
MD_CODE_BLOCK_REGEXP = /^# ```.*?^# ```/m
|
6
|
+
|
7
|
+
BEGIN_KEY = "BEGIN_SRC"
|
8
|
+
END_KEY = "END_SRC"
|
9
|
+
SOURCE_BLOCK_REGEXP = /^#\+#{BEGIN_KEY}.*?^#\+#{END_KEY}/m
|
10
|
+
|
11
|
+
NORMAL_BLOCK_REGEXP = /.*?#{SEPARATOR}/m
|
12
|
+
|
13
|
+
PARAGRAPH_REGEXP = Regexp.union [
|
14
|
+
MD_CODE_BLOCK_REGEXP,
|
15
|
+
SOURCE_BLOCK_REGEXP,
|
16
|
+
NORMAL_BLOCK_REGEXP,
|
17
|
+
]
|
18
|
+
|
19
|
+
def initialize(content)
|
20
|
+
@content = content
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_a
|
24
|
+
v = @content
|
25
|
+
if Source2MD.xmp_out_exclude
|
26
|
+
v = v.remove(/^# >>.*$/)
|
27
|
+
end
|
28
|
+
v = v + "\n\n"
|
29
|
+
v = v.scan(PARAGRAPH_REGEXP)
|
30
|
+
v = v.collect(&:strip)
|
31
|
+
v = v.find_all(&:present?)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
module Source2MD
|
2
|
-
class
|
3
|
-
def initialize(
|
4
|
-
@
|
5
|
-
puts "read: #{file}"
|
2
|
+
class Section
|
3
|
+
def initialize(content)
|
4
|
+
@content = content
|
6
5
|
end
|
7
6
|
|
8
7
|
def to_md
|
@@ -12,7 +11,7 @@ module Source2MD
|
|
12
11
|
private
|
13
12
|
|
14
13
|
def elements
|
15
|
-
@elements ||=
|
14
|
+
@elements ||= Scanner.new(@content).to_a.collect do |e|
|
16
15
|
Element.new(e)
|
17
16
|
end
|
18
17
|
end
|
data/lib/source2md/type/base.rb
CHANGED
@@ -9,27 +9,12 @@ module Source2MD
|
|
9
9
|
|
10
10
|
def initialize(element)
|
11
11
|
@element = element
|
12
|
-
|
13
|
-
Source2MD.logger.debug do
|
14
|
-
o = []
|
15
|
-
o << "-" * 80
|
16
|
-
o << self.class.name
|
17
|
-
o << ""
|
18
|
-
o << "head:"
|
19
|
-
o << element.head
|
20
|
-
o << ""
|
21
|
-
o << "in:"
|
22
|
-
o << element.body
|
23
|
-
o << ""
|
24
|
-
o << "out:"
|
25
|
-
o << to_md
|
26
|
-
o << "-" * 80
|
27
|
-
o.compact * "\n"
|
28
|
-
end
|
29
12
|
end
|
30
13
|
|
31
|
-
def to_md
|
32
|
-
|
14
|
+
# def to_md
|
15
|
+
#
|
16
|
+
# to_md
|
17
|
+
# end
|
33
18
|
end
|
34
19
|
end
|
35
20
|
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Source2MD
|
2
|
+
module Type
|
3
|
+
class ElementInclude < Base
|
4
|
+
def self.accept?(element)
|
5
|
+
element.head[:include]
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(...)
|
9
|
+
super
|
10
|
+
|
11
|
+
@user_define_path, @desc = element.head[:include].split(/\s+/, 2)
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_md
|
15
|
+
CodeBlock.new(body, code_block_options).to_md
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def path
|
21
|
+
Pathname(@user_define_path).expand_path
|
22
|
+
end
|
23
|
+
|
24
|
+
def body
|
25
|
+
path.read.strip
|
26
|
+
end
|
27
|
+
|
28
|
+
def code_block_options
|
29
|
+
options = element.head.dup
|
30
|
+
options[:desc] ||= @desc
|
31
|
+
options[:lang] ||= auto_lang
|
32
|
+
options[:name] ||= path.basename
|
33
|
+
options
|
34
|
+
end
|
35
|
+
|
36
|
+
def auto_lang
|
37
|
+
{
|
38
|
+
# ".foo" => "bar",
|
39
|
+
}.fetch(path.extname) {
|
40
|
+
path.extname.scan(/\w+/).first
|
41
|
+
}
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|