source2md 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +200 -36
  3. data/Rakefile +26 -0
  4. data/doc/0100_top.rb +17 -0
  5. data/doc/0110_code_small.rb +23 -0
  6. data/doc/0120_code_long.rb +23 -0
  7. data/doc/0130_title.rb +21 -0
  8. data/doc/0140_md_like_title.rb +21 -0
  9. data/doc/0150_table.rb +35 -0
  10. data/doc/0160_method.rb +37 -0
  11. data/doc/0170_alert.rb +25 -0
  12. data/doc/0180_text.rb +15 -0
  13. data/doc/0190_raw.rb +19 -0
  14. data/doc/setup.rb +4 -0
  15. data/examples/element_alert.rb +22 -0
  16. data/examples/element_deep_comment.rb +12 -2
  17. data/examples/element_include.rb +20 -0
  18. data/examples/element_md_header.rb +18 -2
  19. data/examples/element_md_title.rb +14 -0
  20. data/examples/element_method.rb +27 -3
  21. data/examples/element_partial_code.rb +22 -0
  22. data/examples/element_reject.rb +11 -0
  23. data/examples/element_source_block.rb +28 -0
  24. data/examples/element_table.rb +18 -6
  25. data/examples/element_text.rb +14 -0
  26. data/examples/element_title.rb +24 -0
  27. data/examples/element_warn.rb +22 -0
  28. data/examples/input.rb +11 -7
  29. data/examples/input.yml +3 -0
  30. data/examples/output.md +7 -22
  31. data/examples/scanner.rb +24 -0
  32. data/examples/setup.rb +1 -1
  33. data/lib/source2md/cli.rb +19 -3
  34. data/lib/source2md/code_block.rb +21 -2
  35. data/lib/source2md/element.rb +36 -13
  36. data/lib/source2md/generator.rb +4 -4
  37. data/lib/source2md/logger.rb +3 -0
  38. data/lib/source2md/scanner.rb +34 -0
  39. data/lib/source2md/{file_block.rb → section.rb} +4 -5
  40. data/lib/source2md/tasks/about.rake +1 -1
  41. data/lib/source2md/text_helper.rb +1 -1
  42. data/lib/source2md/type/base.rb +4 -2
  43. data/lib/source2md/type/element_alert.rb +17 -0
  44. data/lib/source2md/type/element_else.rb +18 -0
  45. data/lib/source2md/type/element_include.rb +45 -0
  46. data/lib/source2md/type/element_md_title.rb +13 -0
  47. data/lib/source2md/type/element_method.rb +1 -1
  48. data/lib/source2md/type/{element_pre.rb → element_partial_code.rb} +2 -2
  49. data/lib/source2md/type/element_source_block.rb +25 -0
  50. data/lib/source2md/type/element_table.rb +4 -4
  51. data/lib/source2md/type/element_title.rb +35 -0
  52. data/lib/source2md/type/element_uncomment.rb +24 -0
  53. data/lib/source2md/type/element_warn.rb +19 -0
  54. data/lib/source2md/version.rb +1 -1
  55. data/lib/source2md.rb +12 -5
  56. data/source2md.gemspec +8 -8
  57. data/spec/code_block_spec.rb +1 -1
  58. data/spec/scanner_spec.rb +26 -0
  59. data/spec/spec_helper.rb +2 -1
  60. data/spec/text_helper_spec.rb +15 -0
  61. data/spec/type/element_alert_spec.rb +16 -0
  62. data/spec/type/element_include_spec.rb +40 -0
  63. data/spec/type/element_md_header_spec.rb +1 -17
  64. data/spec/type/{element_title2_spec.rb → element_md_title_spec.rb} +3 -3
  65. data/spec/type/element_method_spec.rb +5 -5
  66. data/spec/type/{element_pre_spec.rb → element_partial_code_spec.rb} +3 -3
  67. data/spec/type/element_reject_spec.rb +1 -1
  68. data/spec/type/element_source_block_spec.rb +31 -0
  69. data/spec/type/element_table_spec.rb +14 -1
  70. data/spec/type/element_text_spec.rb +1 -1
  71. data/spec/type/{element_deep_comment_spec.rb → element_title_spec.rb} +3 -5
  72. data/spec/type/element_warn_spec.rb +16 -0
  73. data/spec/type/sample.yml +1 -0
  74. metadata +70 -32
  75. data/.rspec +0 -1
  76. data/examples/element_pre.rb +0 -8
  77. data/examples/element_title2.rb +0 -6
  78. data/lib/source2md/type/__element_header_comment.rb +0 -40
  79. data/lib/source2md/type/element_text.rb +0 -26
  80. data/lib/source2md/type/element_title2.rb +0 -16
@@ -1,8 +1,8 @@
1
1
  module Source2MD
2
2
  module Type
3
- class ElementPre < Base
3
+ class ElementPartialCode < Base
4
4
  def self.accept?(element)
5
- element.body.present?
5
+ !element.body.empty?
6
6
  end
7
7
 
8
8
  def to_md
@@ -0,0 +1,25 @@
1
+ module Source2MD
2
+ module Type
3
+ class ElementSourceBlock < Base
4
+ def self.accept?(element)
5
+ element.body.start_with?("#+BEGIN_SRC")
6
+ end
7
+
8
+ def to_md
9
+ CodeBlock.new(body, desc: code_block_desc).to_md
10
+ end
11
+
12
+ private
13
+
14
+ def body
15
+ element.body.match(/#\+BEGIN_SRC.*?\R(.*)#\+END_SRC/m).captures.first.strip
16
+ end
17
+
18
+ def code_block_desc
19
+ if md = element.body.match(/#\+BEGIN_SRC(.+)/)
20
+ md.captures.first.strip
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -2,14 +2,14 @@ module Source2MD
2
2
  module Type
3
3
  class ElementTable < Base
4
4
  def self.accept?(element)
5
- element.body.lines.all? { |e| e.match?(/^# \|.*\|$/) }
5
+ !element.body.empty? && element.body.lines.all? { |e| e.match?(/^# \|.*\|$/) }
6
6
  end
7
7
 
8
8
  def to_md
9
9
  element.body
10
- .gsub(/^# /, "")
11
- .sub(/\A\|.*?\|\R?/, "")
12
- .sub(/^\|.*?\|\z/, "")
10
+ .remove(/^# /)
11
+ .remove(/\A\|-.*?-\|\R?/)
12
+ .remove(/^\|-.*?-\|\z/)
13
13
  .gsub(/-\+-/, "-|-").strip
14
14
  end
15
15
  end
@@ -0,0 +1,35 @@
1
+ module Source2MD
2
+ module Type
3
+ class ElementTitle < Base
4
+ REGEXP = /\Atitle(\d+)\z/
5
+
6
+ def self.accept?(element)
7
+ element.head.keys.any? { |e| e.match?(REGEXP) }
8
+ end
9
+
10
+ def to_md
11
+ s = [prefix, body, prefix] * " "
12
+ Source2MD.logger.warn { s }
13
+ s
14
+ end
15
+
16
+ private
17
+
18
+ def body
19
+ element.head[key]
20
+ end
21
+
22
+ def level
23
+ key.to_s.remove(/\D+/).to_i
24
+ end
25
+
26
+ def prefix
27
+ "#" * level
28
+ end
29
+
30
+ def key
31
+ @key ||= element.head.keys.find { |e| e.match?(REGEXP) }
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,24 @@
1
+ module Source2MD
2
+ module Type
3
+ class ElementUncomment < Base
4
+ REGEXP = /^#( |$)/
5
+
6
+ def self.accept?(element)
7
+ if element.head.empty?
8
+ # "# xxx\n" or "#\n"
9
+ element.body.lines.all? { |e| e.match?(REGEXP) }
10
+ end
11
+ end
12
+
13
+ def to_md
14
+ body
15
+ end
16
+
17
+ private
18
+
19
+ def body
20
+ element.body.remove(REGEXP)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,19 @@
1
+ module Source2MD
2
+ module Type
3
+ class ElementWarn < Base
4
+ def self.accept?(element)
5
+ if element.body.empty?
6
+ element.head[:warn]
7
+ end
8
+ end
9
+
10
+ def to_md
11
+ [
12
+ ":::message",
13
+ element.head[:warn],
14
+ ":::",
15
+ ] * "\n"
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Source2MD
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/source2md.rb CHANGED
@@ -1,22 +1,29 @@
1
+ require "active_support/logger"
2
+ require "active_support/isolated_execution_state"
3
+
1
4
  require "active_support/core_ext/hash"
5
+ require "active_support/core_ext/string"
6
+
2
7
  require "active_support/configurable"
3
- require "zeitwerk"
4
- require "table_format"
8
+
5
9
  require "pathname"
6
10
  require "thor"
11
+ require "table_format"
7
12
 
8
13
  module Source2MD
9
14
  include ActiveSupport::Configurable
10
15
  config_accessor(:debug) { false }
11
- config_accessor(:quiet) { false }
16
+ config_accessor(:xmp_out_exclude) { false }
12
17
  end
13
18
 
19
+ require "zeitwerk"
14
20
  loader = Zeitwerk::Loader.for_gem
21
+ loader.ignore("#{__dir__}/source2md/logger.rb")
15
22
  loader.ignore("#{__dir__}/source2md/**/_*.rb")
16
-
17
23
  loader.inflector.inflect("source2md" => "Source2MD")
18
-
19
24
  loader.log! if false
20
25
  loader.setup
21
26
 
27
+ require "source2md/logger"
28
+
22
29
  loader.eager_load if true
data/source2md.gemspec CHANGED
@@ -1,6 +1,6 @@
1
- lib = File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path("../lib", __FILE__)
2
2
  $LOAD_PATH.unshift(lib) if !$LOAD_PATH.include?(lib)
3
- require 'source2md/version'
3
+ require "source2md/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "source2md"
@@ -18,14 +18,14 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "pathname"
23
+ spec.add_dependency "table_format"
24
+ spec.add_dependency "thor"
25
+ spec.add_dependency "zeitwerk"
26
+
21
27
  spec.add_development_dependency "bundler"
22
28
  spec.add_development_dependency "rake"
23
29
  spec.add_development_dependency "rspec"
24
30
  spec.add_development_dependency "test-unit"
25
-
26
- spec.add_dependency "zeitwerk"
27
- spec.add_dependency "activesupport"
28
- spec.add_dependency "pathname"
29
- spec.add_dependency "thor"
30
- spec.add_dependency "table_format"
31
31
  end
@@ -9,7 +9,7 @@ module Source2MD
9
9
  bb # => 2
10
10
  cccc
11
11
  EOS
12
- assert { s == <<~EOS.strip }
12
+ s.should == <<~EOS.strip
13
13
  ```ruby
14
14
  a # => 1
15
15
 
@@ -0,0 +1,26 @@
1
+ require "spec_helper"
2
+
3
+ module Source2MD
4
+ describe Scanner do
5
+ it "works" do
6
+ ary = Scanner.new(<<~EOS).to_a
7
+ foo
8
+
9
+ #+BEGIN_SRC
10
+
11
+ foo
12
+
13
+ #+END_SRC
14
+
15
+ foo
16
+
17
+ #+BEGIN_SRC
18
+ foo
19
+ #+END_SRC
20
+ foo
21
+ EOS
22
+
23
+ assert { ary == ["foo", "#+BEGIN_SRC\n\nfoo\n\n#+END_SRC", "foo", "#+BEGIN_SRC\nfoo\n#+END_SRC", "foo"] }
24
+ end
25
+ end
26
+ end
data/spec/spec_helper.rb CHANGED
@@ -8,7 +8,8 @@ Pathname.glob("#{__dir__}/support/**/*.rb") { |e| require e.to_s }
8
8
 
9
9
  Zeitwerk::Loader.eager_load_all
10
10
 
11
- Source2MD.quiet = true
11
+ Source2MD.logger.level = :error
12
+ Source2MD.xmp_out_exclude = true
12
13
 
13
14
  RSpec.configure do |config|
14
15
  config.expect_with :rspec do |c|
@@ -0,0 +1,15 @@
1
+ require "spec_helper"
2
+
3
+ module Source2MD
4
+ describe TextHelper do
5
+ it "blank_lines_squish" do
6
+ assert { TextHelper.blank_lines_squish("A\nB\n\nC\n\n\nD") == "A\nB\n\nC\n\nD" }
7
+ end
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" }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ module Source2MD
4
+ describe do
5
+ it "works" do
6
+ actual = Element.new(<<~EOS).to_md
7
+ #+alert: (foo)
8
+ EOS
9
+ actual.should == <<~EOS.strip
10
+ :::message alert
11
+ (foo)
12
+ :::
13
+ EOS
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ module Source2MD
4
+ describe do
5
+ it "works" do
6
+ actual = Element.new(<<~EOS).to_md
7
+ #+include: #{__dir__}/sample.yml
8
+ EOS
9
+ actual.should == <<~EOS.strip
10
+ ```yml:sample.yml
11
+ yaml content
12
+ ```
13
+ EOS
14
+ end
15
+
16
+ it "works" do
17
+ actual = Element.new(<<~EOS).to_md
18
+ #+include: #{__dir__}/sample.yml
19
+ #+lang: (lang)
20
+ #+name: (name)
21
+ EOS
22
+ actual.should == <<~EOS.strip
23
+ ```(lang):(name)
24
+ yaml content
25
+ ```
26
+ EOS
27
+ end
28
+
29
+ it "works" do
30
+ actual = Element.new(<<~EOS).to_md
31
+ #+include: #{__dir__}/sample.yml yaml:filename
32
+ EOS
33
+ actual.should == <<~EOS.strip
34
+ ```yaml:filename
35
+ yaml content
36
+ ```
37
+ EOS
38
+ end
39
+ end
40
+ end
@@ -8,7 +8,7 @@ module Source2MD
8
8
  foo: 1
9
9
  ---
10
10
  EOS
11
- assert { actual == <<~EOS.strip }
11
+ actual.should == <<~EOS.strip
12
12
  ---
13
13
  foo: 1
14
14
  ---
@@ -16,19 +16,3 @@ foo: 1
16
16
  end
17
17
  end
18
18
  end
19
- # >> F
20
- # >>
21
- # >> Failures:
22
- # >>
23
- # >> 1) works
24
- # >> Failure/Error: Unable to find - to read failed line
25
- # >> Test::Unit::AssertionFailedError:
26
- # >> # -:14:in `block (2 levels) in <module:Source2MD>'
27
- # >>
28
- # >> Finished in 0.0113 seconds (files took 0.26182 seconds to load)
29
- # >> 1 example, 1 failure
30
- # >>
31
- # >> Failed examples:
32
- # >>
33
- # >> rspec -:5 # works
34
- # >>
@@ -4,10 +4,10 @@ module Source2MD
4
4
  describe do
5
5
  it "works" do
6
6
  actual = Element.new(<<~EOS).to_md
7
- # title2: foo
7
+ ## foo ##
8
8
  EOS
9
- assert { actual == <<~EOS.strip }
10
- ## foo
9
+ actual.should == <<~EOS.strip
10
+ ## foo ##
11
11
  EOS
12
12
  end
13
13
  end
@@ -4,15 +4,15 @@ module Source2MD
4
4
  describe do
5
5
  it "works" do
6
6
  actual = Element.new(<<~EOS).to_md
7
- # name: (name)
8
- # desc: (desc)
9
- # comment: (comment)
7
+ #+name: (name)
8
+ #+desc: (desc)
9
+ #+comment: (comment)
10
10
  (code1)
11
11
  #
12
12
  (code2)
13
13
  EOS
14
- assert { actual == <<~EOS.strip }
15
- ### (name)
14
+ actual.should == <<~EOS.strip
15
+ ### (name) ###
16
16
 
17
17
  (desc)
18
18
 
@@ -4,11 +4,11 @@ module Source2MD
4
4
  describe do
5
5
  it "works" do
6
6
  actual = Element.new(<<~EOS).to_md
7
- code
7
+ (foo)
8
8
  EOS
9
- assert { actual == <<~EOS.strip }
9
+ actual.should == <<~EOS.strip
10
10
  ```ruby
11
- code
11
+ (foo)
12
12
  ```
13
13
  EOS
14
14
  end
@@ -6,7 +6,7 @@ module Source2MD
6
6
  actual = Element.new(<<~EOS).to_md
7
7
  require "./setup"
8
8
  EOS
9
- assert { actual.blank? }
9
+ assert { actual.nil? }
10
10
  end
11
11
  end
12
12
  end
@@ -0,0 +1,31 @@
1
+ require "spec_helper"
2
+
3
+ module Source2MD
4
+ describe do
5
+ it "works" do
6
+ actual = Element.new(<<~EOS).to_md
7
+ #+BEGIN_SRC
8
+ (foo)
9
+ #+END_SRC
10
+ EOS
11
+ actual.should == <<~EOS.strip
12
+ ```ruby
13
+ (foo)
14
+ ```
15
+ EOS
16
+ end
17
+
18
+ it "works" do
19
+ actual = Element.new(<<~EOS).to_md
20
+ #+BEGIN_SRC diff xxx:yyy
21
+ (foo)
22
+ #+END_SRC
23
+ EOS
24
+ actual.should == <<~EOS.strip
25
+ ```diff xxx:yyy
26
+ (foo)
27
+ ```
28
+ EOS
29
+ end
30
+ end
31
+ end
@@ -11,11 +11,24 @@ module Source2MD
11
11
  # | d | d | d |
12
12
  # |---+---+---|
13
13
  EOS
14
- assert { actual == <<~EOS.strip }
14
+ actual.should == <<~EOS.strip
15
15
  | h | h | h |
16
16
  |---|---|---|
17
17
  | d | d | d |
18
18
  | d | d | d |
19
+ EOS
20
+ end
21
+
22
+ it "works" do
23
+ actual = Element.new(<<~EOS).to_md
24
+ # | h | h | h |
25
+ # |---+---+---|
26
+ # | d | d | d |
27
+ EOS
28
+ actual.should == <<~EOS.strip
29
+ | h | h | h |
30
+ |---|---|---|
31
+ | d | d | d |
19
32
  EOS
20
33
  end
21
34
  end
@@ -8,7 +8,7 @@ module Source2MD
8
8
  # http://example.com/
9
9
  # - bar: http://example.com/
10
10
  EOS
11
- assert { actual == <<~EOS.strip }
11
+ actual.should == <<~EOS.strip
12
12
  - foo
13
13
  http://example.com/
14
14
  - bar: http://example.com/
@@ -4,12 +4,10 @@ module Source2MD
4
4
  describe do
5
5
  it "works" do
6
6
  actual = Element.new(<<~EOS).to_md
7
- ## foo
7
+ #+title2: (foo)
8
8
  EOS
9
- assert { actual == <<~EOS.strip }
10
- ```ruby
11
- # foo
12
- ```
9
+ actual.should == <<~EOS.strip
10
+ ## (foo) ##
13
11
  EOS
14
12
  end
15
13
  end
@@ -0,0 +1,16 @@
1
+ require "spec_helper"
2
+
3
+ module Source2MD
4
+ describe do
5
+ it "works" do
6
+ actual = Element.new(<<~EOS).to_md
7
+ #+warn: (foo)
8
+ EOS
9
+ actual.should == <<~EOS.strip
10
+ :::message
11
+ (foo)
12
+ :::
13
+ EOS
14
+ end
15
+ end
16
+ end
@@ -0,0 +1 @@
1
+ yaml content