riseup 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/riseup.rb +3 -3
- data/lib/riseup/parser.rb +66 -64
- data/lib/riseup/spec.rb +32 -36
- data/lib/riseup/token_data.rb +25 -31
- 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: 262f76dd7d5ed89f0f5c013c0a726ce31bbcd3914751b57df38b23ac0dd92b10
|
4
|
+
data.tar.gz: b9a606d88d6a3ed9e74b74b06af5a74b2b3cb7fb3598157f31b9597c4711cb15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7db234e3c7ef5553792125ee8edcc43599b98c7c7379d2b3bde146821d387025906114159d7e7f5c209dda707a1bb591c47323299a39fe8dc217cec2478f7e6c
|
7
|
+
data.tar.gz: e01c704f20524a660414ee325d50480be0ecb2f2736221822f585c047ac26ad10e1a161247f02e04cba8f79ffb4e4bc3235bd88182c68d2535a07ea164d00f78
|
data/lib/riseup.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'riseup/token_data'
|
2
|
+
require 'riseup/spec'
|
3
|
+
require 'riseup/parser'
|
data/lib/riseup/parser.rb
CHANGED
@@ -1,72 +1,74 @@
|
|
1
1
|
require 'set'
|
2
2
|
module Riseup
|
3
|
-
class Parser
|
4
|
-
#This is a super basic markup language, none of the characters here are unsafe for HTML
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
3
|
+
class Parser
|
4
|
+
# This is a super basic markup language, none of the characters here are unsafe for HTML
|
5
|
+
@@default_spec = Spec.new([
|
6
|
+
# Escapes
|
7
|
+
['\\*', '*'],
|
8
|
+
['\\=', '='],
|
9
|
+
['\\`', '`'],
|
10
|
+
['\\\\', '\\'],
|
11
|
+
# Bold
|
12
|
+
['**', '<b>', '</b>'],
|
13
|
+
# Italic
|
14
|
+
['*', '<i>', '</i>'],
|
15
|
+
# Header
|
16
|
+
['=', '<h1>', '</h2>'],
|
17
|
+
# Code
|
18
|
+
['`', '<code>', '</code>'],
|
19
|
+
# Newline
|
20
|
+
["\n", '<br/>']
|
21
|
+
])
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
def initialize(unformated, spec = @@default_spec)
|
24
|
+
unless unformated.is_a?(String)
|
25
|
+
raise ArgumentError, 'Unformated text (first argument) must be a String'
|
26
|
+
end
|
27
27
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
28
|
+
unless spec.is_a?(Spec)
|
29
|
+
raise ArgumentError, 'Markup specification (second argument) must be a Riseup::Spec'
|
30
|
+
end
|
31
|
+
@unformated = unformated
|
32
|
+
@spec = spec
|
33
|
+
tokens
|
34
|
+
end
|
35
35
|
|
36
|
-
|
37
|
-
|
38
|
-
|
36
|
+
def tokens
|
37
|
+
@tokens = @unformated.split(@spec.regex).reject(&:empty?)
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
42
|
-
tokens()
|
43
|
-
end
|
44
|
-
token_toggle=Set.new
|
45
|
-
new_html=Array.new
|
46
|
-
@tokens.each { |t|
|
47
|
-
if (@spec.include?(t))
|
48
|
-
if (token_toggle.include?(t))
|
49
|
-
token_toggle.delete(t)
|
50
|
-
if (!@spec[t].substitute?())
|
51
|
-
new_html.append(@spec[t].finish)
|
52
|
-
end
|
53
|
-
else
|
54
|
-
if (!@spec[t].substitute?())
|
55
|
-
token_toggle.add(t)
|
56
|
-
new_html.append(@spec[t].start)
|
57
|
-
else
|
58
|
-
new_html.append(@spec[t].substitute)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
else
|
62
|
-
new_html.append(t)
|
63
|
-
end
|
64
|
-
}
|
65
|
-
new_html.join()
|
66
|
-
end
|
40
|
+
def parse
|
41
|
+
tokens if @tokens.nil?
|
67
42
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
43
|
+
token_toggle = Set.new
|
44
|
+
new_html = []
|
45
|
+
@tokens.each do |t|
|
46
|
+
if @spec.include?(t)
|
47
|
+
if token_toggle.include?(t)
|
48
|
+
token_toggle.delete(t)
|
49
|
+
new_html.append(@spec[t].finish) unless @spec[t].substitute?
|
50
|
+
else
|
51
|
+
if !@spec[t].substitute?
|
52
|
+
token_toggle.add(t)
|
53
|
+
new_html.append(@spec[t].start)
|
54
|
+
else
|
55
|
+
new_html.append(@spec[t].substitute)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
else
|
59
|
+
new_html.append(t)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Fix unterminated tokens
|
64
|
+
token_toggle.reverse_each do |token|
|
65
|
+
new_html.append(@spec[token].finish) unless @spec[token].substitute?
|
66
|
+
end
|
67
|
+
new_html.join
|
68
|
+
end
|
69
|
+
|
70
|
+
def to_s
|
71
|
+
@tokens.to_s
|
72
|
+
end
|
73
|
+
end
|
72
74
|
end
|
data/lib/riseup/spec.rb
CHANGED
@@ -1,43 +1,39 @@
|
|
1
1
|
require 'set'
|
2
2
|
|
3
3
|
module Riseup
|
4
|
-
class Spec
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
4
|
+
class Spec
|
5
|
+
def initialize(spec)
|
6
|
+
unless spec.is_a?(Array)
|
7
|
+
raise ArgumentError, 'Specification must be an array'
|
8
|
+
end
|
9
|
+
spec.each_with_index do |i, j|
|
10
|
+
unless i.is_a?(Array)
|
11
|
+
raise ArgumentError, "Specification item #{j} must be an array"
|
12
|
+
end
|
13
|
+
if i.size < 2
|
14
|
+
raise ArgumentError, "Specification item #{j} is too small, must contain at least 2 items"
|
15
|
+
end
|
16
|
+
if i.size > 3
|
17
|
+
raise ArgumentError, "Specification item #{j} is too largw, must contain no more than 3 items"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
@spec = spec
|
21
|
+
@spec_map = Hash[@spec.collect { |t| [t[0], TokenData.new(t[1], t[2])] }]
|
22
|
+
@tokens = @spec.map { |t| t[0] }
|
23
|
+
# (?x)
|
24
|
+
@regex = Regexp.new('(' + (@tokens.map { |t| Regexp.escape(t) }).join('|') + '|[.])')
|
25
|
+
end
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
def include?(x)
|
32
|
-
@spec_map.include?(x)
|
33
|
-
end
|
27
|
+
def [](key)
|
28
|
+
@spec_map[key]
|
29
|
+
end
|
34
30
|
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
def include?(x)
|
32
|
+
@spec_map.include?(x)
|
33
|
+
end
|
38
34
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end
|
35
|
+
attr_reader :regex
|
36
|
+
|
37
|
+
attr_reader :tokens
|
38
|
+
end
|
43
39
|
end
|
data/lib/riseup/token_data.rb
CHANGED
@@ -1,37 +1,31 @@
|
|
1
1
|
require 'set'
|
2
2
|
class TokenData
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
3
|
+
def initialize(start, finish = nil)
|
4
|
+
if finish.nil?
|
5
|
+
@substitute = start
|
6
|
+
@isSub = true
|
7
|
+
else
|
8
|
+
@start = start
|
9
|
+
@finish = finish
|
10
|
+
@isSub = false
|
11
|
+
end
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
@substitute
|
16
|
-
end
|
17
|
-
|
18
|
-
def start()
|
19
|
-
@start
|
20
|
-
end
|
14
|
+
attr_reader :substitute
|
21
15
|
|
22
|
-
|
23
|
-
@finish
|
24
|
-
end
|
16
|
+
attr_reader :start
|
25
17
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
18
|
+
attr_reader :finish
|
19
|
+
|
20
|
+
def substitute?
|
21
|
+
@isSub
|
22
|
+
end
|
23
|
+
|
24
|
+
def all
|
25
|
+
if substitute?
|
26
|
+
[@substitute]
|
27
|
+
else
|
28
|
+
[@start, @finish]
|
29
|
+
end
|
30
|
+
end
|
37
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: riseup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Faissal Bensefia
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email: faissaloo@firemail.cc
|