ppz 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/asset/style/ppz.css +68 -0
  3. data/asset/style/ppz.styl +59 -0
  4. data/bin/common.rb +43 -0
  5. data/bin/ppz +13 -7
  6. data/lib/func/util.rb +10 -1
  7. data/lib/{object/model → model}/abstract/model.rb +16 -18
  8. data/lib/model/abstract/wrapper-model.rb +26 -0
  9. data/lib/model/comment/container.rb +7 -0
  10. data/lib/model/comment/item.rb +12 -0
  11. data/lib/{object/model → model}/common/escape.rb +1 -1
  12. data/lib/{object/model → model}/common/tag.rb +0 -0
  13. data/lib/model/list/item/abstract.rb +13 -0
  14. data/lib/model/list/item/unordered.rb +7 -0
  15. data/lib/model/list/wrapper/abstract.rb +12 -0
  16. data/lib/model/list/wrapper/unordered.rb +3 -0
  17. data/lib/model/p/index.rb +17 -0
  18. data/lib/model/section/abstract.rb +15 -0
  19. data/lib/model/section/leaf.rb +29 -0
  20. data/lib/model/section/root.rb +16 -0
  21. data/lib/model/special-block/container.rb +18 -0
  22. data/lib/{object/model → model}/special-block/item.rb +1 -3
  23. data/lib/{object/parser → parser}/common/context/abstract.rb +3 -5
  24. data/lib/parser/common/context/doc.rb +15 -0
  25. data/lib/parser/doc/abstract.rb +68 -0
  26. data/lib/{object/parser → parser}/doc/file.rb +1 -3
  27. data/lib/{object/parser → parser}/doc/string.rb +1 -3
  28. data/lib/{object/parser → parser}/folder/index.rb +0 -0
  29. data/lib/ppz.rb +26 -1
  30. metadata +27 -26
  31. data/bin/ppz-util.rb +0 -31
  32. data/lib/object/model/abstract/wrapper-model.rb +0 -21
  33. data/lib/object/model/common/tag.test.rb +0 -25
  34. data/lib/object/model/list/item/abstract.rb +0 -15
  35. data/lib/object/model/list/item/unordered.rb +0 -5
  36. data/lib/object/model/list/wrapper/abstract.rb +0 -7
  37. data/lib/object/model/list/wrapper/unordered.rb +0 -5
  38. data/lib/object/model/p/index.rb +0 -15
  39. data/lib/object/model/section/abstract.rb +0 -10
  40. data/lib/object/model/section/leaf.rb +0 -35
  41. data/lib/object/model/section/root.rb +0 -7
  42. data/lib/object/model/section/test.rb +0 -12
  43. data/lib/object/model/special-block/container.rb +0 -18
  44. data/lib/object/parser/common/context/doc.rb +0 -15
  45. data/lib/object/parser/doc/abstract.rb +0 -69
  46. data/lib/object/parser/doc/file.test.rb +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6ecf116f7998efdb6f554563f6b5e9c447872cc3c8d4531fbdba899b986bfcf
4
- data.tar.gz: 545155a99ebdca8a31cd3ec012c2e6a7971a379bd9b97eba7d867c755930a377
3
+ metadata.gz: 50d999d54bdbf78556f37bac10260a0af9315e3b7c708f5e923140628ccbf56e
4
+ data.tar.gz: 2a5a604e6f6fdb05c0652322a35cb1231c3701157a189256c87e34fbd692c251
5
5
  SHA512:
6
- metadata.gz: 5743fdd2c32cca4a9a0e5a67859de962af84252cc1bbceac2a697a724bf49ea8941951824bb504e112d6788b29ba21cd6fa3713a2ec49fe0c4ae565a326968c2
7
- data.tar.gz: 3ec750c9c3580309128c5057e787523933997b3a1108e653b4ce6fbf3222c976772ba9e5439856e3b78ddb2703f6a75e06facf04504befffdcc49be47210ad21
6
+ metadata.gz: 9f205f57824a0bef8e2f2284135e7da87373eb73cb11a4c635315564e707614328f08d6a34950b4f66bfca32de93b990e6dece6384e94580ff99eabee9f5faaa
7
+ data.tar.gz: fe94d84a488ebd411acdc724bcbfe4035011e7948970712b32df71bed3ac2296fde8d98ace6f6c79522ea09e28fb68e8c3616cac80e762ef2fa347e095b65cc3
@@ -0,0 +1,68 @@
1
+ html {
2
+ line-height: 1.5;
3
+ }
4
+ body,
5
+ div,
6
+ p,
7
+ aside {
8
+ box-sizing: border-box;
9
+ }
10
+ body {
11
+ margin: 0;
12
+ padding: 0;
13
+ position: relative;
14
+ }
15
+ aside {
16
+ overflow-y: auto;
17
+ padding: 2rem 1rem;
18
+ position: fixed;
19
+ top: 0;
20
+ left: 0;
21
+ width: 188px;
22
+ height: 100vh;
23
+ }
24
+ aside ul {
25
+ margin: 0 0 0 1rem;
26
+ padding: 0;
27
+ list-style: none;
28
+ }
29
+ aside a {
30
+ width: 100%;
31
+ overflow: hidden;
32
+ text-overflow: ellipsis;
33
+ white-space: nowrap;
34
+ }
35
+ article {
36
+ margin-left: 188px;
37
+ padding: 1px 1rem;
38
+ }
39
+ h1 {
40
+ font-size: 2rem;
41
+ }
42
+ h2 {
43
+ font-size: 1.3rem;
44
+ }
45
+ h3 {
46
+ font-size: 1.1rem;
47
+ opacity: 0.9;
48
+ }
49
+ h1::before,
50
+ h2::before,
51
+ h3::before {
52
+ content: '# ';
53
+ opacity: 0.3;
54
+ }
55
+ .special-block {
56
+ white-space: pre;
57
+ background: #141c22;
58
+ overflow: auto;
59
+ border-radius: 4px;
60
+ color: #eee;
61
+ padding: 1rem;
62
+ }
63
+ .special-txt {
64
+ background: rgba(27,31,35,0.05);
65
+ border-radius: 4px;
66
+ font-size: 0.9em;
67
+ padding: 2px 4px;
68
+ }
@@ -0,0 +1,59 @@
1
+ html
2
+ line-height 1.5
3
+ body, div, p, aside
4
+ box-sizing border-box
5
+
6
+ body
7
+ margin 0
8
+ padding 0
9
+ position relative
10
+
11
+ aside
12
+ overflow-y auto
13
+ padding: 2rem 1rem;
14
+
15
+ position fixed
16
+ top 0
17
+ left 0
18
+ width 188px
19
+ height 100vh
20
+
21
+ ul
22
+ margin 0 0 0 1rem
23
+ padding 0
24
+ list-style none
25
+ a
26
+ width 100%
27
+ overflow hidden
28
+ text-overflow ellipsis
29
+ white-space nowrap
30
+
31
+ article
32
+ margin-left: 188px
33
+ padding: 1px 1rem
34
+ h1
35
+ font-size 2rem
36
+ h2
37
+ font-size 1.3rem
38
+ h3
39
+ font-size 1.1rem
40
+ opacity 0.9
41
+
42
+ h1, h2, h3
43
+ &::before
44
+ content '# '
45
+ opacity 0.3
46
+
47
+ .special-block
48
+ white-space pre
49
+ background #141c22
50
+ overflow auto
51
+ border-radius 4px
52
+ color #eeeeee
53
+ padding 1rem
54
+
55
+ .special-txt
56
+ background rgba(27, 31, 35, 0.05)
57
+ border-radius 4px
58
+ font-size 0.9em
59
+ padding 2px 4px
data/bin/common.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'pathname'
2
+
3
+ module PPZMain
4
+ CURRENT_PATH = File.dirname __FILE__
5
+ WORK_DIRECTORY = Pathname Dir.pwd
6
+ CSS_FILE_PATH = (Pathname CURRENT_PATH) + '../asset/style/ppz.css'
7
+
8
+ class Util
9
+ class << self
10
+ def get_in_and_out
11
+ target_in, target_out = ARGV
12
+
13
+ # + 输入文件
14
+ abort '要编译哪那个文件?请告诉我' unless target_in # 检查参数存在
15
+ target_in = (WORK_DIRECTORY + target_in).to_s # 绝对路径
16
+ unless File.exist? target_in # 不存在的话,看看加上 .ppz 后是否存在
17
+ target_in += '.ppz'
18
+ abort target_in[0..-5] + ' 不存在' unless File.exist? target_in # 还不存在的话,就说明是写错了
19
+ end
20
+ abort '尚未支持编译文件夹' if File.directory? target_in
21
+
22
+ # + 输出文件
23
+ unless target_out
24
+ # 从输入文件获取文件名
25
+ target_out = (/(.*).ppz$/.match target_in)?$1:target_in
26
+ end
27
+ target_out = WORK_DIRECTORY + target_out
28
+ # ++ 检查上级文件夹是否存在
29
+ upper_dir = (target_out + '..').to_s
30
+ abort upper_dir + ' 目录不存在' unless Dir.exist? upper_dir
31
+ # ++ 检查文件夹:有则检查里面有没有文件;无则创建文件夹
32
+ target_out = target_out.to_s
33
+ if Dir.exist? target_out
34
+ abort target_out + ' 不是一个空文件夹' unless (Dir.children target_out).size == 0
35
+ else
36
+ Dir.mkdir target_out
37
+ end
38
+
39
+ [target_in, target_out]
40
+ end
41
+ end
42
+ end
43
+ end
data/bin/ppz CHANGED
@@ -1,12 +1,18 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require_relative './ppz-util'
4
- require_relative '../lib/object/parser/doc/file'
5
- require_relative '../lib/func/util'
3
+ require_relative '../lib/ppz'
4
+ require_relative './common'
5
+ require 'fileutils'
6
+ require 'pathname'
6
7
 
7
- file_in, file_out = UtilInMain.get_file_in_and_out
8
+ target_in, target_out = PPZMain::Util.get_in_and_out
8
9
 
9
- parser = FileDocParser.new path
10
- model = parser.get_model
10
+ target_out = Pathname target_out
11
+ output_html_path = target_out + 'index.html'
12
+ output_css_path = target_out + 'index.css'
11
13
 
12
- Func::write_to_file file_out, model.to_html
14
+ parser = PPZ::FileDocParser.new target_in
15
+ model = parser.get_model
16
+ output_html = '<link rel="stylesheet" href="./index.css"/>' + model.to_html
17
+ PPZ::Func::write_to_file output_html_path.to_s, output_html
18
+ FileUtils.cp PPZMain::CSS_FILE_PATH, output_css_path
data/lib/func/util.rb CHANGED
@@ -1,4 +1,4 @@
1
- class Func
1
+ class PPZ::Func
2
2
  class << self
3
3
  def write_to_file filepath, data
4
4
  if File.exist? filepath
@@ -8,5 +8,14 @@ class Func
8
8
  file.print data
9
9
  file.close
10
10
  end
11
+
12
+ # 检查某类有没有某常量
13
+ def has_const? klass, const_name
14
+ klass.constants.include? const_name
15
+ end
16
+ # 检查某实例的类有没有某常量
17
+ def class_has_const? instance, const_name
18
+ has_const? instance.class, const_name
19
+ end
11
20
  end
12
21
  end
@@ -1,19 +1,17 @@
1
- require_relative '../common/escape'
1
+ class PPZ::AbstractModel
2
+ attr_accessor :left_model, :right_model, :index # 左右兄弟 model
3
+ attr_accessor :father_model
2
4
 
3
- class AbstractModel
4
5
  def self.from_line line # 静态方法,从“输入行”里实例化一个 model
5
- if match_data = self::REG_EXP.match(line)
6
- self.new match_data.post_match
7
- else
8
- nil
9
- end
6
+ return nil unless self::REG_EXP.match(line)
7
+ self.new $1
10
8
  end
11
9
 
12
10
  # 加粗、斜体、链接等
13
11
  def transform_inline_element str
14
12
  str = str + ''
15
13
  # 因为加粗、斜体等,会生成 html 代码,为了不使“用户原本输入的 html”和“生成的 html”冲突,因此先把“用户输入的 html 转义
16
- Escape.html_char! str
14
+ PPZ::Escape.html_char! str
17
15
 
18
16
  # 变粗等,使用特殊字符比如 *,来标识
19
17
  # 但这会使用户想输入 * 时,形成歧义
@@ -24,21 +22,21 @@ class AbstractModel
24
22
  # 再识别哪些变斜,哪些变粗
25
23
  # 再把用户原来想输入的 * 放到字符串里(某种形式 -> *)
26
24
 
27
- Escape.ppz_char! str # 把用户输入的 \* 转义 => 剩下的 *** 就肯定是 斜体加粗 了
25
+ PPZ::Escape.ppz_char! str # 把用户输入的 \* 转义 => 剩下的 *** 就肯定是 斜体加粗 了
28
26
 
29
- # 1. 斜体和加粗
27
+ # + 斜体和加粗
30
28
  str.gsub! /\*\*\*(.+)\*\*\*/, '<b><i>\1</i></b>'
31
- # 2. 加粗
29
+ # + 加粗
32
30
  str.gsub! /\*\*(.+)\*\*/, '<b>\1</b>'
33
- # 3. 斜体
31
+ # + 斜体
34
32
  str.gsub! /\*(.+)\*/, '<i>\1</i>'
35
- # 4. 行内块
36
- str.gsub! /```(.+)```/, '<span class="special-txt">\1</span>'
37
- # 5. 链接
38
- str.gsub! /\[([^\]]+)\]\(([^\)]+)\)/, '<a href="\2" title="\2">\1</a>'
39
- # 6. 图片
33
+ # + 行内块
34
+ str.gsub! /```([^(```)]+)```/, '<span class="special-txt">\1</span>'
35
+ # + 图片 先图片后链接
40
36
  str.gsub! /!\[([^\]]*)\]\(([^\)]+)\)/, '<img title="\1" src="\2" />'
37
+ # + 链接 先图片后链接
38
+ str.gsub! /\[([^\]]+)\]\(([^\)]+)\)/, '<a href="\2" title="\2">\1</a>'
41
39
 
42
- Escape.transform_to_real! str
40
+ PPZ::Escape.transform_to_real! str
43
41
  end
44
42
  end
@@ -0,0 +1,26 @@
1
+ class PPZ::AbstractWrapperModel < PPZ::AbstractModel
2
+ def initialize
3
+ @children = []
4
+ end
5
+
6
+ # 把 el 加入到 children
7
+ def append el
8
+ el.father_model = self
9
+
10
+ left_model = @children[-1]
11
+ if left_model
12
+ left_model.right_model = el
13
+ el.left_model = el
14
+ el.index = left_model.index + 1
15
+ else
16
+ el.index = 1
17
+ end
18
+ @children.push el
19
+ end
20
+
21
+ def to_html
22
+ @children
23
+ .map { |child| child.to_html }
24
+ .join
25
+ end
26
+ end
@@ -0,0 +1,7 @@
1
+ class PPZ::CommentContainerModel < PPZ::AbstractWrapperModel
2
+ UpperClass = PPZ::AbstractSectionModel
3
+
4
+ def to_html
5
+ "<div class=\"comment\">#{super}</div>"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ class PPZ::CommentItemModel < PPZ::AbstractModel
2
+ ContainerClass = PPZ::CommentContainerModel
3
+ REG_EXP = /^\> (.+)/
4
+
5
+ def initialize content
6
+ @content = transform_inline_element content
7
+ end
8
+
9
+ def to_html
10
+ "<div>#{@content}</div>"
11
+ end
12
+ end
@@ -1,4 +1,4 @@
1
- class Escape
1
+ class PPZ::Escape
2
2
  PpzRule = [
3
3
  ['\\\\', '\\backslash', '\\'],
4
4
  ['\\*', '\\star;', '*'],
File without changes
@@ -0,0 +1,13 @@
1
+ class PPZ::AbstractListItemModel < PPZ::AbstractModel
2
+ attr_reader :level
3
+
4
+ def initialize text, level
5
+ super()
6
+ @text = transform_inline_element text
7
+ @level = level
8
+ end
9
+
10
+ def to_html
11
+ "<li>#{@text}</li>"
12
+ end
13
+ end
@@ -0,0 +1,7 @@
1
+ class PPZ::UnorderedListItemModel < PPZ::AbstractListItemModel
2
+ REG_EXP = /^(\++) (.+)/
3
+ def self.from_line line
4
+ return nil unless REG_EXP.match(line)
5
+ self.new $2, $1.size
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ class PPZ::AbstractListWrapperModel < PPZ::AbstractWrapperModel
2
+ attr_reader :level
3
+
4
+ def initialize level
5
+ super()
6
+ @level = level
7
+ end
8
+
9
+ def to_html
10
+ "<#{self.class::TAG_NAME}>#{super}</#{self.class::TAG_NAME}>"
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class PPZ::UnorderedListWrapperModel < PPZ::AbstractListWrapperModel
2
+ TAG_NAME = 'ul'
3
+ end
@@ -0,0 +1,17 @@
1
+ class PPZ::PModel < PPZ::AbstractModel
2
+ UpperClass = PPZ::AbstractSectionModel
3
+
4
+ def initialize text
5
+ # 转义行首的加号
6
+ pre = text[0..2]
7
+ text = text[1..-1] if (pre == '\\+ ' or pre == '\\> ')
8
+ # 转义 html
9
+ text = transform_inline_element text
10
+
11
+ @text = text
12
+ end
13
+
14
+ def to_html
15
+ "<p>#{@text}</p>"
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ class PPZ::AbstractSectionModel < PPZ::AbstractWrapperModel
2
+ def append section
3
+ super
4
+ if section.is_a? PPZ::AbstractSectionModel
5
+ section.section_dom_id = "#{section_dom_id}-#{section.level.to_s}.#{section.index.to_s}"
6
+ end
7
+ end
8
+
9
+ def get_nav_html
10
+ @children
11
+ .select { |child| child.is_a? PPZ::AbstractSectionModel }
12
+ .collect { |child| child.get_nav_html }
13
+ .join
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ class PPZ::LeafSectionModel < PPZ::AbstractSectionModel
2
+ attr_accessor :title, :section_dom_id, :level
3
+
4
+ def initialize title, level
5
+ raise TypeError unless title.is_a?(String) && level.is_a?(Integer)
6
+ super() # 不可以省略括号
7
+ @title = transform_inline_element title
8
+ @level = level
9
+ end
10
+
11
+ REG_EXP = /^(\#{1,5}) (.+)/
12
+ def self.from_line line
13
+ return nil unless REG_EXP.match(line)
14
+
15
+ level = {
16
+ 1 => 1, # 一个井号是 一级
17
+ 5 => 3 # 五个井号是 三级
18
+ }[$1.size] || 2 # 其余都是 两级
19
+ PPZ::LeafSectionModel.new $2, level
20
+ end
21
+
22
+ def get_nav_html
23
+ return "<li><a href=\"##{section_dom_id}\">#{@title}</a><ul>#{super}</ul></li>"
24
+ end
25
+
26
+ def to_html
27
+ "<section id=#{@section_dom_id}><h#{@level}>#{@title}</h#{@level}>#{super}</section>"
28
+ end
29
+ end
@@ -0,0 +1,16 @@
1
+ class PPZ::RootSectionModel < PPZ::AbstractSectionModel
2
+ def level
3
+ 0
4
+ end
5
+ def section_dom_id
6
+ 'section'
7
+ end
8
+
9
+ def get_nav_html
10
+ return "<aside><ul>#{super}</ul></aside>"
11
+ end
12
+
13
+ def to_html
14
+ "#{get_nav_html}<article>#{super}</article>"
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ class PPZ::SpecialContainerModel < PPZ::AbstractWrapperModel
2
+ UpperClass = PPZ::AbstractSectionModel
3
+
4
+ REG_EXP = /^```( (.*))?/
5
+ def self.from_line line
6
+ return nil unless REG_EXP.match(line)
7
+ PPZ::SpecialContainerModel.new $2
8
+ end
9
+
10
+ def initialize name
11
+ @name = name
12
+ super()
13
+ end
14
+
15
+ def to_html
16
+ "<div class=\"special-block #{@name}\">#{super}</div>"
17
+ end
18
+ end
@@ -1,6 +1,4 @@
1
- require_relative '../abstract/model'
2
-
3
- class SpecialItemModel < AbstractModel
1
+ class PPZ::SpecialItemModel < PPZ::AbstractModel
4
2
  def initialize line
5
3
  @line = line
6
4
  end
@@ -1,15 +1,13 @@
1
1
  # 当前行,所处的上下文
2
2
  # 比如 一级 section 下面的 ul 下的 第 n 个 ul
3
3
 
4
- class AbstractContext
4
+ class PPZ::AbstractContext
5
5
  def initialize root
6
6
  @stack = [root]
7
7
  end
8
8
 
9
- def append target
10
- @stack.push target
11
- end
12
-
9
+ # def append # 交给子类实现
10
+
13
11
  def pop
14
12
  @stack.pop
15
13
  end
@@ -0,0 +1,15 @@
1
+ class PPZ::DocContext < PPZ::AbstractContext
2
+ def append target
3
+ # ContainerClass: 容器类,如果上级不是,就造一个
4
+ if(PPZ::Func::class_has_const? target, :ContainerClass) and (head.class != target.class::ContainerClass)
5
+ append target.class::ContainerClass.new
6
+ end
7
+ # UpperClass: 上级类,如果上级不是,就出栈
8
+ if(PPZ::Func::class_has_const? target, :UpperClass) and (head.class != target.class::UpperClass)
9
+ pop_to target.class::UpperClass
10
+ end
11
+
12
+ head.append target # 加入 model
13
+ @stack.push target if target.is_a? PPZ::AbstractWrapperModel # 加入 stack
14
+ end
15
+ end
@@ -0,0 +1,68 @@
1
+ # 解析一个 .ppz 文档(可以是一个文件、字符串)
2
+
3
+ class PPZ::AbstractDocParser
4
+ def initialize
5
+ @context = PPZ::DocContext.new PPZ::RootSectionModel.new
6
+ end
7
+
8
+ def get_model
9
+ loop do
10
+ line = readline
11
+ break unless line != nil
12
+ handle_line line
13
+ end
14
+ @context.root
15
+ end
16
+
17
+ private
18
+ def handle_line line
19
+ head = @context.head
20
+ if head.is_a? PPZ::SpecialContainerModel # 只要进入 special-block,下面的 line 都算是 special-block 的content
21
+ # special-block
22
+ if /^``` *$/.match line # 除非遇到 ``` (special-block 的结束符)
23
+ # special-block-end
24
+ @context.pop # 遇到,就跳出去
25
+ return # 立刻结束
26
+ elsif /\\``` *$/.match line # 但是有的 ``` (是 special-block 的内容,于是需要转义)
27
+ line = line[1..-1]
28
+ end
29
+ # special-block-item
30
+ target = PPZ::SpecialItemModel.new line
31
+ elsif target = PPZ::LeafSectionModel.from_line(line)
32
+ # section
33
+ loop do
34
+ break if (head.is_a? PPZ::AbstractSectionModel) and (head.level < target.level)
35
+ @context.pop
36
+ head = @context.head
37
+ end
38
+ elsif target = PPZ::UnorderedListItemModel.from_line(line)
39
+ # 列表
40
+ # + 对上级 container 的操作
41
+ # ++ 没有 container -> new
42
+ # ++ 有,等级低 -> new
43
+ # ++ 有,等级高 -> pop 到同等级,如果没有同等级,则 new
44
+ # ++ 有,等级相等 -> 啥也不做
45
+ if !(head.is_a? PPZ::UnorderedListWrapperModel) || head.level < target.level
46
+ @context.append PPZ::UnorderedListWrapperModel.new target.level
47
+ elsif head.level > target.level
48
+ loop do
49
+ @context.pop
50
+ head = @context.head
51
+ break if head.level <= target.level # pop 到同等级
52
+ end
53
+ if head.level < target.level # 如果没有同等级,则 new
54
+ @context.append PPZ::UnorderedListWrapperModel.new target.level
55
+ end
56
+ end
57
+ elsif target = PPZ::CommentItemModel.from_line(line)
58
+ # 注释
59
+ elsif target = PPZ::SpecialContainerModel.from_line(line)
60
+ # 特殊快
61
+ else
62
+ # p
63
+ target = PPZ::PModel.new line
64
+ end
65
+
66
+ @context.append target
67
+ end
68
+ end
@@ -1,6 +1,4 @@
1
- require_relative './abstract'
2
-
3
- class FileDocParser < AbstractDocParser
1
+ class PPZ::FileDocParser < PPZ::AbstractDocParser
4
2
  def initialize path
5
3
  super()
6
4
  unless File.exist? path
@@ -1,6 +1,4 @@
1
- require_relative './abstract'
2
-
3
- class StringDocParser < AbstractDocParser
1
+ class PPZ::StringDocParser < PPZ::AbstractDocParser
4
2
  def initialize str
5
3
  super()
6
4
  @lines = str.split /\n/
File without changes
data/lib/ppz.rb CHANGED
@@ -1 +1,26 @@
1
- p '接口还没写呢~感兴趣的话,可以直接读源码,码量不大'
1
+ module PPZ
2
+ require_relative './func/util'
3
+
4
+ require_relative './model/abstract/model'
5
+ require_relative './model/abstract/wrapper-model'
6
+ require_relative './model/section/abstract'
7
+ require_relative './model/section/leaf'
8
+ require_relative './model/section/root'
9
+ require_relative './model/comment/container'
10
+ require_relative './model/comment/item'
11
+ require_relative './model/common/escape'
12
+ require_relative './model/list/wrapper/abstract'
13
+ require_relative './model/list/wrapper/unordered'
14
+ require_relative './model/list/item/abstract'
15
+ require_relative './model/list/item/unordered'
16
+ require_relative './model/p/index'
17
+ require_relative './model/special-block/container'
18
+ require_relative './model/special-block/item'
19
+
20
+ require_relative './parser/common/context/abstract'
21
+ require_relative './parser/common/context/doc'
22
+ require_relative './parser/doc/abstract'
23
+ require_relative './parser/doc/file'
24
+ require_relative './parser/doc/string'
25
+
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ppz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - wuse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-02 00:00:00.000000000 Z
11
+ date: 2021-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: 372301467@qq.com
@@ -17,32 +17,33 @@ executables:
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - asset/style/ppz.css
21
+ - asset/style/ppz.styl
22
+ - bin/common.rb
20
23
  - bin/ppz
21
- - bin/ppz-util.rb
22
24
  - lib/func/util.rb
23
- - lib/object/model/abstract/model.rb
24
- - lib/object/model/abstract/wrapper-model.rb
25
- - lib/object/model/common/escape.rb
26
- - lib/object/model/common/tag.rb
27
- - lib/object/model/common/tag.test.rb
28
- - lib/object/model/list/item/abstract.rb
29
- - lib/object/model/list/item/unordered.rb
30
- - lib/object/model/list/wrapper/abstract.rb
31
- - lib/object/model/list/wrapper/unordered.rb
32
- - lib/object/model/p/index.rb
33
- - lib/object/model/section/abstract.rb
34
- - lib/object/model/section/leaf.rb
35
- - lib/object/model/section/root.rb
36
- - lib/object/model/section/test.rb
37
- - lib/object/model/special-block/container.rb
38
- - lib/object/model/special-block/item.rb
39
- - lib/object/parser/common/context/abstract.rb
40
- - lib/object/parser/common/context/doc.rb
41
- - lib/object/parser/doc/abstract.rb
42
- - lib/object/parser/doc/file.rb
43
- - lib/object/parser/doc/file.test.rb
44
- - lib/object/parser/doc/string.rb
45
- - lib/object/parser/folder/index.rb
25
+ - lib/model/abstract/model.rb
26
+ - lib/model/abstract/wrapper-model.rb
27
+ - lib/model/comment/container.rb
28
+ - lib/model/comment/item.rb
29
+ - lib/model/common/escape.rb
30
+ - lib/model/common/tag.rb
31
+ - lib/model/list/item/abstract.rb
32
+ - lib/model/list/item/unordered.rb
33
+ - lib/model/list/wrapper/abstract.rb
34
+ - lib/model/list/wrapper/unordered.rb
35
+ - lib/model/p/index.rb
36
+ - lib/model/section/abstract.rb
37
+ - lib/model/section/leaf.rb
38
+ - lib/model/section/root.rb
39
+ - lib/model/special-block/container.rb
40
+ - lib/model/special-block/item.rb
41
+ - lib/parser/common/context/abstract.rb
42
+ - lib/parser/common/context/doc.rb
43
+ - lib/parser/doc/abstract.rb
44
+ - lib/parser/doc/file.rb
45
+ - lib/parser/doc/string.rb
46
+ - lib/parser/folder/index.rb
46
47
  - lib/ppz.rb
47
48
  homepage: https://github.com/daGaiGuanYu/ppz
48
49
  licenses:
data/bin/ppz-util.rb DELETED
@@ -1,31 +0,0 @@
1
- require 'pathname'
2
-
3
- class UtilInMain
4
- class << self
5
- def get_file_in_and_out
6
- file_in, file_out = ARGV
7
- unless file_in && file_out
8
- abort '参数不对啊,需要至少俩参数(输入文件路径、输出文件路径)'
9
- end
10
-
11
- current_path = Pathname Dir.pwd
12
- file_in = (current_path + file_in).to_s
13
- file_out = (current_path + file_out).to_s
14
-
15
- unless File.exist? file_in
16
- abort '没有 ' + file_in + " 这个文件\n【参数不正确:ppz 源文件不存在】"
17
- end
18
-
19
- file_out_dir = File.dirname file_out
20
- unless Dir.exist? file_out_dir
21
- abort '输出文件的目录不存在'
22
- end
23
-
24
- if File.exist? file_out
25
- abort '输出文件已存在'
26
- end
27
-
28
- [file_in, file_out]
29
- end
30
- end
31
- end
@@ -1,21 +0,0 @@
1
- require_relative './model'
2
-
3
- class AbstractWrapperModel < AbstractModel
4
- def initialize
5
- @children = []
6
- end
7
-
8
- # 把 el 加入到 children
9
- def append el
10
- if el.respond_to? :on_append
11
- el.on_append
12
- end
13
- @children.push el
14
- end
15
-
16
- def to_html
17
- @children
18
- .map { |child| child.to_html }
19
- .join
20
- end
21
- end
@@ -1,25 +0,0 @@
1
- require_relative './tag'
2
- require_relative 'test/unit'
3
-
4
- class TagTest < Test::Unit::TestCase
5
- def test_1
6
- assert_equal '<div></div>', AbstractModel.new.getTag
7
- end
8
-
9
- def test_2
10
- assert_equal '<div>ha i</div>', AbstractModel.new('ha i').getTag
11
- end
12
-
13
- def test_3
14
- assert_equal '<span>hai</span>', AbstractModel.new('hai', tagName: 'span').getTag
15
- end
16
-
17
- def test_4
18
- assert_equal '<span name=\'test\' tel=\'110\'>hai</span>', AbstractModel.new('hai', tagName: 'span',
19
- attr: {
20
- name: 'test',
21
- tel: '110'
22
- }
23
- ).getTag
24
- end
25
- end
@@ -1,15 +0,0 @@
1
- require_relative '../../abstract/wrapper-model'
2
-
3
- class AbstractListItemModel < AbstractModel
4
- def initialize text
5
- @text = transform_inline_element text
6
- end
7
-
8
- def on_append
9
- puts '+: ' + @text
10
- end
11
-
12
- def to_html
13
- "<li>#{@text}</li>"
14
- end
15
- end
@@ -1,5 +0,0 @@
1
- require_relative './abstract'
2
-
3
- class UnorderedListItemModel < AbstractListItemModel
4
- REG_EXP = /^\++ /
5
- end
@@ -1,7 +0,0 @@
1
- require_relative '../../abstract/model.rb'
2
-
3
- class AbstractListWrapperModel < AbstractWrapperModel
4
- def to_html
5
- "<#{self.class::TAG_NAME}>#{super}</#{self.class::TAG_NAME}>"
6
- end
7
- end
@@ -1,5 +0,0 @@
1
- require_relative './abstract'
2
-
3
- class UnorderedListWrapperModel < AbstractListWrapperModel
4
- TAG_NAME = 'ul'
5
- end
@@ -1,15 +0,0 @@
1
- require_relative '../abstract/model'
2
-
3
- class PModel < AbstractModel
4
- def initialize text
5
- @text = transform_inline_element text
6
- end
7
-
8
- def on_append
9
- puts ' : ' + @text
10
- end
11
-
12
- def to_html
13
- "<p>#{@text}</p>"
14
- end
15
- end
@@ -1,10 +0,0 @@
1
- require_relative '../abstract/wrapper-model'
2
-
3
- class AbstractSectionModel < AbstractWrapperModel
4
- def append section
5
- if section.is_a? AbstractSectionModel
6
- section.father = self
7
- end
8
- super
9
- end
10
- end
@@ -1,35 +0,0 @@
1
- require_relative './abstract'
2
-
3
- class LeafSectionModel < AbstractSectionModel
4
-
5
- attr_accessor :title, :father
6
- attr_reader :level
7
-
8
- def initialize title, level
9
- raise TypeError unless title.is_a?(String) && level.is_a?(Integer)
10
- super() # 不可以省略括号
11
- @title = transform_inline_element title
12
- @level = level
13
- end
14
-
15
- REG_EXP = /^\#{1,5} /
16
- def self.from_line line
17
- return nil unless match_data = REG_EXP.match(line)
18
-
19
- level = {
20
- 2 => 1,
21
- 6 => 3
22
- }[match_data.to_s.length] || 2
23
- LeafSectionModel.new match_data.post_match, level
24
- end
25
-
26
- def on_append
27
- puts 'h: ' + @title
28
- end
29
-
30
- def to_html
31
- %{<section><h#{@level}>#{@title}</h#{@level}>#{
32
- super
33
- }</section>}
34
- end
35
- end
@@ -1,7 +0,0 @@
1
- require_relative './abstract'
2
-
3
- class RootSectionModel < AbstractSectionModel
4
- def level
5
- 0
6
- end
7
- end
@@ -1,12 +0,0 @@
1
- require 'test/unit'
2
- require_relative './leaf'
3
- require_relative './root'
4
-
5
- root = RootSectionModel.new
6
- leaf1 = LeafSectionModel.new '一级标题'
7
- leaf2 = LeafSectionModel.new '第二个一级标题'
8
- leaf11 = LeafSectionModel.new '二级标题'
9
-
10
- root.append leaf1
11
- leaf1.append leaf11
12
- root.append leaf2
@@ -1,18 +0,0 @@
1
- require_relative '../abstract/wrapper-model'
2
-
3
- class SpecialContainerModel < AbstractWrapperModel
4
- REG_EXP = /^``` (.+)/
5
- def self.from_line line
6
- return nil unless match_data = REG_EXP.match(line)
7
- SpecialContainerModel.new $1
8
- end
9
-
10
- def initialize name
11
- @name = name
12
- super()
13
- end
14
-
15
- def to_html
16
- "<div class=\"special-block #{@name}\">#{super}</div>"
17
- end
18
- end
@@ -1,15 +0,0 @@
1
- require_relative '../../../model/section/abstract'
2
- require_relative './abstract'
3
-
4
- class DocContext < AbstractContext
5
- def level
6
- @stack.reverse_each do |item|
7
- return item.level if item.is_a? AbstractSectionModel
8
- end
9
- throw '不应该到这里'
10
- end
11
-
12
- def pop_to_section
13
- pop_to AbstractSectionModel
14
- end
15
- end
@@ -1,69 +0,0 @@
1
- # 解析一个 .ppz 文档(可以是一个文件、字符串)
2
-
3
- require_relative '../common/context/doc'
4
- require_relative '../../model/section/leaf'
5
- require_relative '../../model/section/root'
6
- require_relative '../../model/p/index'
7
- require_relative '../../model/list/item/unordered'
8
- require_relative '../../model/list/wrapper/unordered'
9
- require_relative '../../model/special-block/container'
10
- require_relative '../../model/special-block/item'
11
-
12
- class AbstractDocParser
13
- def initialize
14
- @context = DocContext.new RootSectionModel.new
15
- end
16
-
17
- def get_model
18
- loop do
19
- line = readline
20
- break unless line != nil
21
- handle_line line
22
- end
23
- @context.root
24
- end
25
-
26
- private
27
- def handle_line line
28
- if @context.head.is_a? SpecialContainerModel
29
- # special-block
30
- if /^``` *$/.match line
31
- # special-block-end
32
- @context.pop
33
- return
34
- else
35
- # special-block-item
36
- target = SpecialItemModel.new line
37
- end
38
- elsif target = SpecialContainerModel.from_line(line)
39
- # special-block-container
40
- elsif target = LeafSectionModel.from_line(line)
41
- # section
42
- # 检查 level
43
- loop do
44
- break if @context.level < target.level
45
- @context.pop
46
- end
47
- elsif target = UnorderedListItemModel.from_line(line)
48
- # 列表
49
- unless @context.head.is_a? UnorderedListWrapperModel # 如果当前不在一个 无序列表 里
50
- wrapper = UnorderedListWrapperModel.new # 就整一个无序列表
51
- @context.pop_to_section # 找到最近的 section
52
- @context.head.append wrapper # 加入 wrapper
53
- @context.append wrapper # wrapper 入上下文栈
54
- end
55
- # p
56
- else
57
- @context.pop_to_section # 找到最近的 section
58
- target = PModel.new line
59
- end
60
-
61
- # 添加到父级 model
62
- @context.head.append target
63
-
64
- # 推入上下文
65
- if target.is_a? AbstractWrapperModel
66
- @context.append target
67
- end
68
- end
69
- end
@@ -1,6 +0,0 @@
1
- require_relative './file'
2
-
3
- target = FileDocParser.new __FILE__
4
-
5
- p target.readline;p target.readline;p target.readline;p target.readline;
6
- p target.readline;p target.readline;p target.readline;p target.readline;