nishisuke_blog_syntax 0.1.0 → 0.1.8

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: cfa1ccb8a04c5f3b29bc6848c1743bb5441052caadb7629825e2710e0b81ee61
4
- data.tar.gz: 29ddef7d14cbb251cd446d20aeaa636b745ed6d495f6ab5e747033e5924d401b
3
+ metadata.gz: 3276579e5d936933ed684fa6910535298f972df0b1a0d91469bb0facef9ba6e8
4
+ data.tar.gz: 896d09a79820022427ac924d520e0906f50113358dd1cceeb5f2b7e12b09321f
5
5
  SHA512:
6
- metadata.gz: 4f3a75974ea41e3d0a79b820925206399fff1c4d75bb6bd60524fb20436d97cd9842758f5269207c3ddbe615af680d711cd2cb38e7336588428a3ac8c53635aa
7
- data.tar.gz: a69f9f8bf57a879a9f35cfbf0e225cb9edde1a4d0b7975bb7def38c96f3046b2b2e61c463090db48b672d722092b56db3fbf4fa9f595c541874875224f069f7c
6
+ metadata.gz: 2628a7287dc8fc772892b27616e897ea81e89f24c849e7d58a0e2ae677bccbe54dac5eabe862f11c862e84afde239b65e0c067326cf1d3582816404012245771
7
+ data.tar.gz: 06dd0e38619c46f314f3f9dc729f67f601fe3c374ca1b36f5de4a838c5153a29e72a373e0bacb56cce2c47beb098b381ac1a3db1dbeaadaa00a5e9d5f6546fd9
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.5.0
@@ -0,0 +1,45 @@
1
+ require 'nishisuke_blog_syntax/formatter'
2
+ require 'erb'
3
+ include ERB::Util
4
+
5
+ module NishisukeBlogSyntax
6
+ module Formatter
7
+ class CodeFormatter < FormatterBase
8
+ SUBSTITUTE_REGEXP = /^SRC```(.*?)SRC```$/m
9
+ PARSE_REGEXP = /^SRC```(.*)SRC```$/m
10
+
11
+ private
12
+
13
+ def regexp
14
+ SUBSTITUTE_REGEXP
15
+ end
16
+
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
24
+
25
+ def wrapped_line(txt)
26
+ %Q(<span class="shell__code-line">#{h(txt)}</span>)
27
+ end
28
+
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
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,21 @@
1
+ require 'nishisuke_blog_syntax/formatter'
2
+
3
+ module NishisukeBlogSyntax
4
+ module Formatter
5
+ class HeadThreeFormatter < FormatterBase
6
+ SUBSTITUTE_REGEXP = /^### .*$/
7
+ PARSE_REGEXP = /^### (.*)$/
8
+
9
+ private
10
+
11
+ def regexp
12
+ SUBSTITUTE_REGEXP
13
+ end
14
+
15
+ def substitute(matched)
16
+ content_str = matched.match(PARSE_REGEXP)[1]
17
+ "<h3>#{content_str}</h3>"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ require 'nishisuke_blog_syntax/formatter'
2
+
3
+ module NishisukeBlogSyntax
4
+ module Formatter
5
+ class ListFormatter < FormatterBase
6
+ SUBSTITUTE_REGEXP = /^- .*$/
7
+ PARSE_REGEXP = /^- (.*)$/
8
+
9
+ private
10
+
11
+ def regexp
12
+ SUBSTITUTE_REGEXP
13
+ end
14
+
15
+ def substitute(matched)
16
+ content_str = matched.match(PARSE_REGEXP)[1]
17
+ "<li>#{content_str}</li>"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ require 'nishisuke_blog_syntax/formatter'
2
+
3
+ module NishisukeBlogSyntax
4
+ module Formatter
5
+ class ParagraphFormatter < FormatterBase
6
+ SUBSTITUTE_REGEXP = /^>>>(.*?)<<<$/m
7
+ PARSE_REGEXP = /^>>>(.*)<<<$/m
8
+
9
+ private
10
+
11
+ def regexp
12
+ SUBSTITUTE_REGEXP
13
+ end
14
+
15
+ def substitute(matched)
16
+ content_str = matched.match(PARSE_REGEXP)[1]
17
+ content_str.gsub!(/\R|\s+/, ' ')
18
+ "<p>#{content_str.chop}</p>"
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,51 @@
1
+ require 'nishisuke_blog_syntax/formatter'
2
+ require 'erb'
3
+ include ERB::Util
4
+
5
+ module NishisukeBlogSyntax
6
+ module Formatter
7
+ class ShellFormatter < FormatterBase
8
+ SUBSTITUTE_REGEXP = /^SHELL```(.*?)SHELL```$/m
9
+ PARSE_REGEXP = /^SHELL```(.*)SHELL```$/m
10
+ INPUT_REGEXP = /\Ain: /
11
+ OUTPUT_REGEXP = /\Aout: /
12
+
13
+ private
14
+
15
+ def regexp
16
+ SUBSTITUTE_REGEXP
17
+ end
18
+
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
26
+
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.
34
+ end
35
+ end
36
+
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", '')
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ require 'nishisuke_blog_syntax/formatter'
2
+
3
+ module NishisukeBlogSyntax
4
+ module Formatter
5
+ class SubHeadlineFormatter < FormatterBase
6
+ SUBSTITUTE_REGEXP = /^## .*$/
7
+ PARSE_REGEXP = /^## (.*)$/
8
+
9
+ private
10
+
11
+ def regexp
12
+ SUBSTITUTE_REGEXP
13
+ end
14
+
15
+ def substitute(matched)
16
+ content_str = matched.match(PARSE_REGEXP)[1]
17
+ "<h2>#{content_str}</h2>"
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module NishisukeBlogSyntax
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
18
+ end
19
+ end
@@ -0,0 +1,38 @@
1
+ require 'nishisuke_blog_syntax/formatter/code_formatter'
2
+ require 'nishisuke_blog_syntax/formatter/shell_formatter'
3
+ require 'nishisuke_blog_syntax/formatter/paragraph_formatter'
4
+ require 'nishisuke_blog_syntax/formatter/sub_headline_formatter'
5
+ require 'nishisuke_blog_syntax/formatter/head_three_formatter'
6
+ require 'nishisuke_blog_syntax/formatter/list_formatter'
7
+
8
+ module NishisukeBlogSyntax
9
+ class RawText
10
+ def initialize(text)
11
+ @text = text.gsub(/\R/, "\n")
12
+ end
13
+
14
+ def to_html
15
+ self.class.html_formatters.inject(text) do |html, f|
16
+ f.format(html)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def self.html_formatters
23
+ # order is important!
24
+ # Each formatter should be commutative.
25
+ # But be careful order for the future.
26
+ @@formatters ||= [
27
+ Formatter::ShellFormatter.new,
28
+ Formatter::CodeFormatter.new,
29
+ Formatter::ParagraphFormatter.new,
30
+ Formatter::SubHeadlineFormatter.new,
31
+ Formatter::HeadThreeFormatter.new,
32
+ Formatter::ListFormatter.new,
33
+ ]
34
+ end
35
+
36
+ attr_reader :text
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module NishisukeBlogSyntax
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.8'
3
3
  end
@@ -1,5 +1,11 @@
1
- require "nishisuke_blog_syntax/version"
1
+ require 'nishisuke_blog_syntax/version'
2
+ require 'nishisuke_blog_syntax/raw_text'
2
3
 
3
4
  module NishisukeBlogSyntax
4
- # Your code goes here...
5
+ class << self
6
+ def convert_html(txt)
7
+ raw = RawText.new(txt)
8
+ raw.to_html
9
+ end
10
+ end
5
11
  end
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require "nishisuke_blog_syntax/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "nishisuke_blog_syntax"
7
+ spec.name = 'nishisuke_blog_syntax'
8
8
  spec.version = NishisukeBlogSyntax::VERSION
9
9
  spec.authors = ["nishisuke"]
10
10
  spec.email = ["nishisuke13+git@gmail.com"]
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.0
4
+ version: 0.1.8
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-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,6 +60,7 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - ".ruby-version"
63
64
  - ".travis.yml"
64
65
  - Gemfile
65
66
  - LICENSE.txt
@@ -68,6 +69,14 @@ files:
68
69
  - bin/console
69
70
  - bin/setup
70
71
  - lib/nishisuke_blog_syntax.rb
72
+ - lib/nishisuke_blog_syntax/formatter.rb
73
+ - lib/nishisuke_blog_syntax/formatter/code_formatter.rb
74
+ - lib/nishisuke_blog_syntax/formatter/head_three_formatter.rb
75
+ - lib/nishisuke_blog_syntax/formatter/list_formatter.rb
76
+ - lib/nishisuke_blog_syntax/formatter/paragraph_formatter.rb
77
+ - lib/nishisuke_blog_syntax/formatter/shell_formatter.rb
78
+ - lib/nishisuke_blog_syntax/formatter/sub_headline_formatter.rb
79
+ - lib/nishisuke_blog_syntax/raw_text.rb
71
80
  - lib/nishisuke_blog_syntax/version.rb
72
81
  - nishisuke_blog_syntax.gemspec
73
82
  homepage: https://github.com/nishisuke/blog-syntax