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 +4 -4
- data/lib/nishisuke_blog_syntax/formatter/code_formatter.rb +30 -35
- data/lib/nishisuke_blog_syntax/formatter/shell_formatter.rb +33 -39
- data/lib/nishisuke_blog_syntax/formatter.rb +16 -4
- data/lib/nishisuke_blog_syntax/raw_text.rb +10 -6
- data/lib/nishisuke_blog_syntax/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f59386919e11f1189c963eacac6c12106212ca229d681e2df37d85300534d3d
|
4
|
+
data.tar.gz: 4ca04e391a7bc2990ee2070c115923b4018254b85e62f0de22c1c391f8812aed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
8
|
-
|
7
|
+
class CodeFormatter < FormatterBase
|
8
|
+
SUBSTITUTE_REGEXP = /^SRC```(.*?)SRC```$/m
|
9
|
+
PARSE_REGEXP = /^SRC```(.*)SRC```$/m
|
9
10
|
|
10
|
-
|
11
|
-
def format(txt)
|
12
|
-
m = txt.match(REGEXP)
|
13
|
-
return txt if m.nil?
|
11
|
+
private
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
|
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
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
36
|
-
|
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
|
-
|
46
|
-
|
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
|
-
|
8
|
-
|
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
|
-
|
13
|
-
def format(txt)
|
14
|
-
m = txt.match(REGEXP)
|
15
|
-
return txt if m.nil?
|
13
|
+
private
|
16
14
|
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
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
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
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
|
-
|
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
|
11
|
-
|
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::
|
20
|
-
Formatter::
|
23
|
+
Formatter::ShellFormatter.new,
|
24
|
+
Formatter::CodeFormatter.new,
|
21
25
|
]
|
22
26
|
end
|
23
27
|
|
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.
|
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-
|
11
|
+
date: 2018-01-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|