nora_mark 0.2beta3
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.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/CHANGELOG.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +188 -0
- data/Rakefile +12 -0
- data/lib/nora_mark/html/abstract_item_writer.rb +14 -0
- data/lib/nora_mark/html/context.rb +119 -0
- data/lib/nora_mark/html/generator.rb +177 -0
- data/lib/nora_mark/html/header_writer.rb +35 -0
- data/lib/nora_mark/html/pages.rb +56 -0
- data/lib/nora_mark/html/paragraph_writer.rb +52 -0
- data/lib/nora_mark/html/tag_writer.rb +110 -0
- data/lib/nora_mark/html/util.rb +12 -0
- data/lib/nora_mark/html/writer_selector.rb +24 -0
- data/lib/nora_mark/parser.kpeg +123 -0
- data/lib/nora_mark/parser.kpeg.rb +3422 -0
- data/lib/nora_mark/parser.rb +31 -0
- data/lib/nora_mark/version.rb +3 -0
- data/lib/nora_mark.rb +46 -0
- data/nora_mark.gemspec +22 -0
- data/spec/created_files/.gitignore +1 -0
- data/spec/fixture/test_src_ja.nora +50 -0
- data/spec/nokogiri_test_helper.rb +41 -0
- data/spec/nora_mark_spec.rb +840 -0
- data/spec/spec_helper.rb +29 -0
- metadata +116 -0
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'nora_mark/parser.kpeg'
|
2
|
+
|
3
|
+
module NoraMark
|
4
|
+
class Parser
|
5
|
+
def create_item(type, command, children = [], raw: nil)
|
6
|
+
children[0].sub!(/^[[:space:]]+/, '') if !children.nil? && children[0].is_a?(String)
|
7
|
+
item = {:type => type, :children => children, :raw_text => raw }.merge command || {}
|
8
|
+
item[:args] ||= []
|
9
|
+
item[:named_args] = Hash[*(item[:args].select { |x| x.include?(':') }.map { |x| v = x.split(':', 2); [v[0].strip.to_sym, v[1]]}.flatten)]
|
10
|
+
item
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse_text(content)
|
14
|
+
content.inject([]) do
|
15
|
+
|result, item|
|
16
|
+
if item.is_a? String
|
17
|
+
s = result.last
|
18
|
+
if s.is_a? String
|
19
|
+
result.pop
|
20
|
+
else
|
21
|
+
s = ''
|
22
|
+
end
|
23
|
+
result.push s + item
|
24
|
+
else
|
25
|
+
result.push item
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
data/lib/nora_mark.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "nora_mark/version"
|
2
|
+
require 'nora_mark/html/generator'
|
3
|
+
require 'nora_mark/parser'
|
4
|
+
|
5
|
+
module NoraMark
|
6
|
+
class Document
|
7
|
+
private_class_method :new
|
8
|
+
|
9
|
+
def self.parse(string_or_io, param = {})
|
10
|
+
instance = new param
|
11
|
+
src = string_or_io.respond_to?(:read) ? string_or_io.read : string_or_io
|
12
|
+
yield instance if block_given?
|
13
|
+
instance.instance_eval do
|
14
|
+
@preprocessors.each do
|
15
|
+
|pr|
|
16
|
+
src = pr.call(src)
|
17
|
+
end
|
18
|
+
@parser = Parser.new(src)
|
19
|
+
if (!@parser.parse)
|
20
|
+
raise @parser.raise_error
|
21
|
+
end
|
22
|
+
end
|
23
|
+
instance
|
24
|
+
end
|
25
|
+
|
26
|
+
def preprocessor(&block)
|
27
|
+
@preprocessors << block
|
28
|
+
end
|
29
|
+
|
30
|
+
def html
|
31
|
+
if @html.nil?
|
32
|
+
@html = @html_generator.convert(@parser.result)
|
33
|
+
end
|
34
|
+
@html
|
35
|
+
end
|
36
|
+
|
37
|
+
def initialize(param = {})
|
38
|
+
@preprocessors = [
|
39
|
+
Proc.new { |text| text.gsub(/\r?\n(\r?\n)+/, "\n\n") },
|
40
|
+
]
|
41
|
+
@html_generator = Html::Generator.new(param)
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
data/nora_mark.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/nora_mark/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["KOJIMA Satoshi"]
|
6
|
+
gem.email = ["skoji@mac.com"]
|
7
|
+
gem.description = %q{simple and customizable text markup language for EPUB}
|
8
|
+
gem.summary = %q{simple and customizable text markup language for EPUB}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "nora_mark"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = NoraMark::VERSION
|
17
|
+
|
18
|
+
gem.required_ruby_version = '>= 2.0.0'
|
19
|
+
gem.add_dependency "kpeg"
|
20
|
+
gem.add_development_dependency "rspec", "~> 2.14"
|
21
|
+
gem.add_development_dependency "nokogiri", "~> 1.6.0"
|
22
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
*.xhtml
|
@@ -0,0 +1,50 @@
|
|
1
|
+
title: the sample text
|
2
|
+
sect.preface {
|
3
|
+
h1: 前書き
|
4
|
+
前書きのようなものをここでかく。
|
5
|
+
「前書き」とは英語では[s.strong{preface}]のことだろうか。
|
6
|
+
}
|
7
|
+
|
8
|
+
newpage:
|
9
|
+
|
10
|
+
art.main {
|
11
|
+
|
12
|
+
h1: 本文
|
13
|
+
ここからが本文。
|
14
|
+
チェックする項目は
|
15
|
+
|
16
|
+
1: ol要素が出ること
|
17
|
+
2: ul要素がでること
|
18
|
+
3: dl要素
|
19
|
+
4: spanがでること
|
20
|
+
|
21
|
+
である。
|
22
|
+
|
23
|
+
[s.strong{NoraMark}] の要素は、次のものからなる。
|
24
|
+
|
25
|
+
*: 行
|
26
|
+
*: パラグラフ
|
27
|
+
*: 行コマンド
|
28
|
+
*: ブロックコマンド
|
29
|
+
*: インラインコマンド
|
30
|
+
|
31
|
+
;: 行: 行コマンドでマークアップされていない行は、pタグで囲まれる。
|
32
|
+
;: パラグラフ: 空行で区切られた一連の行は、div class='pgroup' で囲まれる。空行ではなく、別のコマンドによってもパラグラフは生成される。
|
33
|
+
|
34
|
+
コマンドについて説明する。
|
35
|
+
|
36
|
+
;:コマンド文字列: <コマンド名>[#id名]*[.クラス名]*[(パラメータ)]
|
37
|
+
;: 行コマンド : 行先頭に、<コマンド文字列>: がくる。続く行末までの文字が処理対象となる。p/h以外のブロック要素を生成する行コマンドは、暗黙のブロックを中断する。
|
38
|
+
;: ブロックコマンド : 行先頭に<コマンド文字列>{ がくる。その行では文字を続けない。} 単独の行で閉じる。
|
39
|
+
;: インラインコマンド : 行の途中にあらわれる[<コマンド文字列>{<文字>}] の列。
|
40
|
+
|
41
|
+
定義済みコマンドは次のとおり。
|
42
|
+
|
43
|
+
;.commands: 行コマンド: p
|
44
|
+
;: パラグラフを中断する行コマンド: newpage, image, h1, h2, h3, h4, h5, h6, *(箇条書き), 数字(列挙), ;(定義リスト)
|
45
|
+
;:ブロックコマンド: d, art, sec。未定義であっても、ブロックコマンドの形式をしていればなんでも<コマンド名> ... </コマンド名> に展開される。
|
46
|
+
;:インラインコマンド: s, l, img。 その他は、インラインコマンドの形式をしていれば、なんでも<コマンド名> ... </コマンド名> に展開される。
|
47
|
+
|
48
|
+
|
49
|
+
|
50
|
+
}
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module Nokogiri
|
2
|
+
module XML
|
3
|
+
class Element
|
4
|
+
def selector
|
5
|
+
sel = name
|
6
|
+
if !self['id'].nil?
|
7
|
+
sel = sel + '#' + self['id'].split(' ').join('#')
|
8
|
+
end
|
9
|
+
if !self['class'].nil?
|
10
|
+
sel = sel + '.' + self['class'].split(' ').join('.')
|
11
|
+
end
|
12
|
+
attributes.select{|k,v| k != 'class' && k != 'id'}.each {
|
13
|
+
|name, value|
|
14
|
+
sel = sel + "[#{name}='#{value}']"
|
15
|
+
}
|
16
|
+
sel
|
17
|
+
end
|
18
|
+
def selector_and_text
|
19
|
+
[selector, text]
|
20
|
+
end
|
21
|
+
alias a selector_and_text
|
22
|
+
def child_loop
|
23
|
+
yield self
|
24
|
+
end
|
25
|
+
def child_a(index)
|
26
|
+
element_children[index].selector_and_text
|
27
|
+
end
|
28
|
+
def selector_and_children
|
29
|
+
[selector] + children.select{|c| c.elem? || c.text.strip.size > 0}.map{|c|
|
30
|
+
if !c.elem?
|
31
|
+
c.text
|
32
|
+
elsif c.element_children.size == 0
|
33
|
+
c.selector_and_text
|
34
|
+
else
|
35
|
+
c.selector_and_children
|
36
|
+
end
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|