nishisuke-blog-syntax 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e13274afecedf181f356c27d03bcd5acc2fd3d3880544ad665ce0727939e096c
4
- data.tar.gz: c0a48dde5caeba5685d43efeac06371716e5e95f1093cf0e82a6400f715f5d3e
3
+ metadata.gz: 0f59386919e11f1189c963eacac6c12106212ca229d681e2df37d85300534d3d
4
+ data.tar.gz: 4ca04e391a7bc2990ee2070c115923b4018254b85e62f0de22c1c391f8812aed
5
5
  SHA512:
6
- metadata.gz: 1978a0a7788860b3e8252acc28fd4ece0840e00fb722d9a9bacc9ec90f5fa8b3a3de458100aa64a1293700d20cd7eaba1c2f3c7dfeb59a1631fd7b3e21bc25b3
7
- data.tar.gz: b2702596de0dc94c44a2d0d4da68f20f080f5cc7860b30081ff4ec0dcabb53805805685cb4891b8dd62392620d123d1a2fee8ef51dc24b1a3d260997dfcef072
6
+ metadata.gz: c7f26032b18f8009018c06bdbb6cc587d6ed910651b5fb8f863ef4e5e70672dc1b6522661262feafff83a624213b4cf4d3cf484f36f064293341d0e184571bd4
7
+ data.tar.gz: a005e9dae3ac33694aefa66bc5759c5f36809ac1bdd1f4f83467a750a87c7d84e485fb350682b118d96da23f5832876ce0e0c042888079c49472c8da55ffd2d5
@@ -4,46 +4,41 @@ include ERB::Util
4
4
 
5
5
  module NishisukeBlogSyntax
6
6
  module Formatter
7
- module CodeFormatter
8
- REGEXP = /^SRC```(.*)SRC```$/m
7
+ class CodeFormatter < FormatterBase
8
+ SUBSTITUTE_REGEXP = /^SRC```(.*?)SRC```$/m
9
+ PARSE_REGEXP = /^SRC```(.*)SRC```$/m
9
10
 
10
- class << self
11
- def format(txt)
12
- m = txt.match(REGEXP)
13
- return txt if m.nil?
11
+ private
14
12
 
15
- contents = m[1].split("\n")
16
-
17
- file_name = contents.shift
18
-
19
- content_str = contents.map { |l| wrapped_line(l) }.join('<br>')
20
- html = wrapped_content(content_str, file_name)
21
-
22
- txt.gsub(REGEXP, html)
23
- end
24
-
25
- private
26
-
27
- def wrapped_line(txt)
28
- %Q(<span class="shell__code-line">#{h(txt)}</span>)
29
- end
13
+ def regexp
14
+ SUBSTITUTE_REGEXP
15
+ end
30
16
 
31
- def wrapped_content(content, file_name)
32
- has_file_name = !file_name.gsub(/\s/, '').empty?
17
+ def substitute(matched)
18
+ content_str = matched.match(PARSE_REGEXP)[1]
19
+ contents = content_str.split("\n")
20
+ file_name = contents.shift
21
+ contents.map! { |l| wrapped_line(l) }
22
+ wrapped_content(contents.join('<br>'), file_name)
23
+ end
33
24
 
34
- html = <<~HTML
35
- <div class="shell mdc-elevation--z2">
36
- #{%Q(<span class="shell__file">#{file_name}</span>) if has_file_name}
37
- <pre class="shell__container">
38
- <code class="shell__code">
39
- #{content}
40
- </code>
41
- </pre>
42
- </div>
43
- HTML
25
+ def wrapped_line(txt)
26
+ %Q(<span class="shell__code-line">#{h(txt)}</span>)
27
+ end
44
28
 
45
- html.gsub("\n", '')
46
- end
29
+ def wrapped_content(content, file_name)
30
+ has_file_name = !file_name.gsub(/\s/, '').empty?
31
+
32
+ html = <<~HTML.gsub("\n", '')
33
+ <div class="shell mdc-elevation--z2">
34
+ #{%Q(<span class="shell__file">#{file_name}</span>) if has_file_name}
35
+ <pre class="shell__container">
36
+ <code class="shell__code">
37
+ #{content}
38
+ </code>
39
+ </pre>
40
+ </div>
41
+ HTML
47
42
  end
48
43
  end
49
44
  end
@@ -4,53 +4,47 @@ include ERB::Util
4
4
 
5
5
  module NishisukeBlogSyntax
6
6
  module Formatter
7
- module ShellFormatter
8
- REGEXP = /^SHELL```(.*)SHELL```$/m
7
+ class ShellFormatter < FormatterBase
8
+ SUBSTITUTE_REGEXP = /^SHELL```(.*?)SHELL```$/m
9
+ PARSE_REGEXP = /^SHELL```(.*)SHELL```$/m
9
10
  INPUT_REGEXP = /\Ain: /
10
11
  OUTPUT_REGEXP = /\Aout: /
11
12
 
12
- class << self
13
- def format(txt)
14
- m = txt.match(REGEXP)
15
- return txt if m.nil?
13
+ private
16
14
 
17
- contents = m[1].split("\n")
18
-
19
- file_name = contents.shift
20
-
21
- content_str = contents.reject(&:empty?).map do |l|
22
- wrapped_line(l)
23
- end.join('<br>')
24
-
25
- html = wrapped_content(content_str)
26
-
27
- txt.gsub(REGEXP, html)
28
- end
15
+ def regexp
16
+ SUBSTITUTE_REGEXP
17
+ end
29
18
 
30
- private
19
+ def substitute(matched)
20
+ content_str = matched.match(PARSE_REGEXP)[1]
21
+ contents = content_str.split("\n")
22
+ contents.shift # drop first line, this is SRC```xxxxx <-
23
+ contents.map! { |l| wrapped_line(l) }
24
+ wrapped_content(contents.join('<br>'))
25
+ end
31
26
 
32
- def wrapped_line(txt)
33
- if m = txt.match(INPUT_REGEXP)
34
- %Q(<kbd class="shell__input">#{h(m.post_match)}</kbd>)
35
- elsif m = txt.match(OUTPUT_REGEXP)
36
- %Q(<samp class="shell__output">#{h(m.post_match)}</samp>)
37
- else
38
- h(txt)
39
- end
27
+ def wrapped_line(txt)
28
+ if m = txt.match(INPUT_REGEXP)
29
+ %Q(<kbd class="shell__input">#{h(m.post_match)}</kbd>)
30
+ elsif m = txt.match(OUTPUT_REGEXP)
31
+ %Q(<samp class="shell__output">#{h(m.post_match)}</samp>)
32
+ else
33
+ '' # this means <br><br> finaly.
40
34
  end
35
+ end
41
36
 
42
- def wrapped_content(content)
43
- html = <<~HTML
44
- <div class="shell mdc-elevation--z2">
45
- <pre class="shell__container">
46
- <span class="shell__std">
47
- #{content}
48
- </span>
49
- </pre>
50
- </div>
51
- HTML
52
- html.gsub("\n", '')
53
- end
37
+ def wrapped_content(content)
38
+ html = <<~HTML
39
+ <div class="shell mdc-elevation--z2">
40
+ <pre class="shell__container">
41
+ <span class="shell__std">
42
+ #{content}
43
+ </span>
44
+ </pre>
45
+ </div>
46
+ HTML
47
+ html.gsub("\n", '')
54
48
  end
55
49
  end
56
50
  end
@@ -1,7 +1,19 @@
1
- require 'nishisuke_blog_syntax/formatter/code_formatter'
2
- require 'nishisuke_blog_syntax/formatter/shell_formatter'
3
-
4
- module BlogFormatParser
1
+ module NishisukeBlogSyntax
5
2
  module Formatter
3
+ class FormatterBase
4
+ def format(txt)
5
+ txt.gsub(regexp) { |matched| substitute(matched) }
6
+ end
7
+
8
+ private
9
+
10
+ def regexp
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def substitute
15
+ raise NotImplementedError
16
+ end
17
+ end
6
18
  end
7
19
  end
@@ -1,23 +1,27 @@
1
- require 'nishisuke_blog_syntax/formatter'
1
+ require 'nishisuke_blog_syntax/formatter/code_formatter'
2
+ require 'nishisuke_blog_syntax/formatter/shell_formatter'
2
3
 
3
4
  module NishisukeBlogSyntax
4
5
  class RawText
5
6
  def initialize(text)
6
- @text = text
7
+ @text = text.gsub(/\R/, "\n")
7
8
  end
8
9
 
9
10
  def to_html
10
- self.class.html_formatters.inject(text.gsub(/\R/, "\n")) do |html, formatter|
11
- formatter.format(html)
11
+ self.class.html_formatters.inject(text) do |html, f|
12
+ f.format(html)
12
13
  end
13
14
  end
14
15
 
15
16
  private
16
17
 
17
18
  def self.html_formatters
19
+ # order is important!
20
+ # Each formatter should be commutative.
21
+ # But be careful order for the future.
18
22
  @@formatters ||= [
19
- Formatter::CodeFormatter,
20
- Formatter::ShellFormatter,
23
+ Formatter::ShellFormatter.new,
24
+ Formatter::CodeFormatter.new,
21
25
  ]
22
26
  end
23
27
 
@@ -1,3 +1,3 @@
1
1
  module NishisukeBlogSyntax
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nishisuke-blog-syntax
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - nishisuke
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-09 00:00:00.000000000 Z
11
+ date: 2018-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler