ppz 0.0.1 → 0.0.6

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 (55) hide show
  1. checksums.yaml +4 -4
  2. data/asset/style/ppz.css +141 -0
  3. data/asset/style/ppz.styl +126 -0
  4. data/bin/common.rb +42 -0
  5. data/bin/doc.rb +17 -0
  6. data/bin/folder.rb +10 -0
  7. data/bin/ppz +12 -10
  8. data/lib/func/util.rb +19 -2
  9. data/lib/{object/model → model}/abstract/model.rb +16 -18
  10. data/lib/model/abstract/wrapper-model.rb +26 -0
  11. data/lib/model/comment/container.rb +7 -0
  12. data/lib/model/comment/item.rb +12 -0
  13. data/lib/{object/model → model}/common/escape.rb +1 -1
  14. data/lib/{object/model → model}/common/tag.rb +0 -0
  15. data/lib/model/list/item/abstract.rb +13 -0
  16. data/lib/model/list/item/unordered.rb +7 -0
  17. data/lib/model/list/wrapper/abstract.rb +12 -0
  18. data/lib/model/list/wrapper/unordered.rb +3 -0
  19. data/lib/model/p/index.rb +17 -0
  20. data/lib/model/section/abstract.rb +15 -0
  21. data/lib/model/section/leaf.rb +29 -0
  22. data/lib/model/section/root.rb +16 -0
  23. data/lib/model/special-block/container.rb +25 -0
  24. data/lib/model/special-block/item.rb +8 -0
  25. data/lib/{object/parser → parser}/common/context/abstract.rb +3 -5
  26. data/lib/parser/common/context/doc.rb +15 -0
  27. data/lib/parser/doc/abstract.rb +68 -0
  28. data/lib/{object/parser → parser}/doc/file.rb +1 -3
  29. data/lib/{object/parser → parser}/doc/string.rb +1 -3
  30. data/lib/parser/folder/index.rb +9 -0
  31. data/lib/parser/folder/model/abstract.rb +90 -0
  32. data/lib/parser/folder/model/file/abstract.rb +23 -0
  33. data/lib/parser/folder/model/file/other.rb +7 -0
  34. data/lib/parser/folder/model/file/ppz.rb +15 -0
  35. data/lib/parser/folder/model/folder.rb +93 -0
  36. data/lib/ppz.rb +26 -1
  37. metadata +34 -26
  38. data/bin/ppz-util.rb +0 -31
  39. data/lib/object/model/abstract/wrapper-model.rb +0 -21
  40. data/lib/object/model/common/tag.test.rb +0 -25
  41. data/lib/object/model/list/item/abstract.rb +0 -15
  42. data/lib/object/model/list/item/unordered.rb +0 -5
  43. data/lib/object/model/list/wrapper/abstract.rb +0 -7
  44. data/lib/object/model/list/wrapper/unordered.rb +0 -5
  45. data/lib/object/model/p/index.rb +0 -15
  46. data/lib/object/model/section/abstract.rb +0 -10
  47. data/lib/object/model/section/leaf.rb +0 -35
  48. data/lib/object/model/section/root.rb +0 -7
  49. data/lib/object/model/section/test.rb +0 -12
  50. data/lib/object/model/special-block/container.rb +0 -18
  51. data/lib/object/model/special-block/item.rb +0 -10
  52. data/lib/object/parser/common/context/doc.rb +0 -15
  53. data/lib/object/parser/doc/abstract.rb +0 -69
  54. data/lib/object/parser/doc/file.test.rb +0 -6
  55. data/lib/object/parser/folder/index.rb +0 -7
@@ -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-container\">#{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 class=\"comment-item\">#{@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,25 @@
1
+ class PPZ::SpecialContainerModel < PPZ::AbstractWrapperModel
2
+ UpperClass = PPZ::AbstractSectionModel
3
+
4
+ REG_EXP = /^```( (.*))?$/
5
+ def self.from_line line
6
+ if REG_EXP.match line
7
+ if $2 && ($2.include? '```')
8
+ nil
9
+ else
10
+ PPZ::SpecialContainerModel.new $2
11
+ end
12
+ else
13
+ nil
14
+ end
15
+ end
16
+
17
+ def initialize name
18
+ @name = name
19
+ super()
20
+ end
21
+
22
+ def to_html
23
+ "<div class=\"special-block-container #{@name}\">#{super}</div>"
24
+ end
25
+ end
@@ -0,0 +1,8 @@
1
+ class PPZ::SpecialItemModel < PPZ::AbstractModel
2
+ def initialize line
3
+ @line = line
4
+ end
5
+ def to_html
6
+ "<div class=\"special-block-item\">#{@line}</div>"
7
+ end
8
+ 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/
@@ -0,0 +1,9 @@
1
+ # 解析一个文件夹里的 .ppz 文件
2
+
3
+ module PPZ::Folder
4
+ require_relative './model/abstract'
5
+ require_relative './model/folder'
6
+ require_relative './model/file/abstract'
7
+ require_relative './model/file/other'
8
+ require_relative './model/file/ppz'
9
+ end
@@ -0,0 +1,90 @@
1
+ module PPZ::Folder
2
+ class AbstractModel
3
+ attr_reader :index, :name, :level
4
+ attr_accessor :prev_model, :next_model, :father_model
5
+
6
+ def initialize path, level
7
+ throw '文件(夹)的名字得是字符串啊' unless path.is_a? String
8
+ @path = path
9
+ @basename = File.basename path
10
+ @level = level
11
+ end
12
+
13
+ def self.from_path path, level
14
+ level += 1
15
+ if Dir.exist? path
16
+ FolderModel.new path, level
17
+ elsif File.exist? path
18
+ AbstractFileModel.from_path path, level
19
+ else
20
+ throw path + '不存在?'
21
+ end
22
+ end
23
+
24
+ def get_css_path
25
+ ('../' * @level) + 'style.css'
26
+ end
27
+
28
+ def relative_link target
29
+ relative_level = target.level - @level # relative_level 是“当前 model 比目标 level 高几级”
30
+
31
+ (if relative_level > 0
32
+ '../' * relative_level
33
+ elsif relative_level < 0
34
+ result = ''
35
+ father = @father_model
36
+ (- relative_level).times do
37
+ result = father.name + '/' + result
38
+ father = father.father_model
39
+ end
40
+ result
41
+ else
42
+ ''
43
+ end) + @name + '.html'
44
+ end
45
+
46
+ private
47
+ def to_html
48
+ get_head_html +
49
+ get_ancestor_html +
50
+ get_content_html +
51
+ get_nav_html
52
+ end
53
+
54
+ def get_head_html
55
+ %~<title>#{@name}</title><link rel="stylesheet" href="#{get_css_path}"/>~
56
+ end
57
+
58
+ def get_ancestor_html
59
+ list = []
60
+ father = self
61
+ loop do
62
+ break unless father.father_model
63
+ father = father.father_model
64
+ list.unshift father
65
+ end
66
+ %~<div class="ancestor-nav"><ul>#{
67
+ (list.collect do |node|
68
+ %`<li><a href="#{node.relative_link self}">#{node.name}</a></li>`
69
+ end
70
+ .join) + %`<li class="self">#{self.name}</li>`
71
+ }</ul></div>~
72
+ end
73
+
74
+ def get_nav_html
75
+ # prev_model nav_html
76
+ prev_model_html = ''
77
+ if @prev_model
78
+ prev_link = @prev_model.relative_link self
79
+ prev_model_html = "<li class=\"prev\"><a href=\"#{prev_link}\">#{@prev_model.name}</a></li>"
80
+ end
81
+ next_model_html = ''
82
+ if @next_model
83
+ next_link = @next_model.relative_link self
84
+ next_model_html = "<li class=\"next\"><a href=\"#{next_link}\">#{@next_model.name}</a></li>"
85
+ end
86
+
87
+ %~<ul class="interpage-nav">#{prev_model_html}#{next_model_html}</ul>~
88
+ end
89
+ end
90
+ end