riseup 1.0.0 → 1.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d8345958f998425fb0d2f39477f9ed24f2da288a2923cd79b8a6aa445c448aa5
4
- data.tar.gz: cb133e4573c71c568387302bb203010bb6f4c8ea24f20f632809768b0a01252e
3
+ metadata.gz: 262f76dd7d5ed89f0f5c013c0a726ce31bbcd3914751b57df38b23ac0dd92b10
4
+ data.tar.gz: b9a606d88d6a3ed9e74b74b06af5a74b2b3cb7fb3598157f31b9597c4711cb15
5
5
  SHA512:
6
- metadata.gz: 9002fdd7106633f9c54744989f8eb7328d8398af0a532c6b186403c2981d4bb7e5b01498ca463add713b86bc25e39c2d1fe170c206909cb0d218f2fa0670cb15
7
- data.tar.gz: 33436b77aefd422db0ae5880bfb646ea929504018c755a4615609742485204fb41d2fe22d2ed3c338914e74c4815a1caa8d8c91d153312fa75d3a50979ed31ce
6
+ metadata.gz: 7db234e3c7ef5553792125ee8edcc43599b98c7c7379d2b3bde146821d387025906114159d7e7f5c209dda707a1bb591c47323299a39fe8dc217cec2478f7e6c
7
+ data.tar.gz: e01c704f20524a660414ee325d50480be0ecb2f2736221822f585c047ac26ad10e1a161247f02e04cba8f79ffb4e4bc3235bd88182c68d2535a07ea164d00f78
@@ -1,3 +1,3 @@
1
- require "riseup/token_data"
2
- require "riseup/spec"
3
- require "riseup/parser"
1
+ require 'riseup/token_data'
2
+ require 'riseup/spec'
3
+ require 'riseup/parser'
@@ -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
- @@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
- ])
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
- def initialize(unformated,spec=@@default_spec)
24
- if (!unformated.is_a?(String))
25
- raise ArgumentError.new("Unformated text (first argument) must be a String")
26
- end
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
- if (!spec.is_a?(Spec))
29
- raise ArgumentError.new("Markup specification (second argument) must be a Riseup::Spec")
30
- end
31
- @unformated=unformated
32
- @spec=spec
33
- tokens()
34
- end
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
- def tokens()
37
- @tokens = @unformated.split(@spec.regex).reject(&:empty?)
38
- end
36
+ def tokens
37
+ @tokens = @unformated.split(@spec.regex).reject(&:empty?)
38
+ end
39
39
 
40
- def parse()
41
- if (@tokens.nil?)
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
- def to_s()
69
- @tokens.to_s()
70
- end
71
- end
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
@@ -1,43 +1,39 @@
1
1
  require 'set'
2
2
 
3
3
  module Riseup
4
- class Spec
5
- def initialize(spec)
6
- if (!spec.is_a?(Array))
7
- raise ArgumentError.new("Specification must be an array")
8
- end
9
- spec.each_with_index { |i,j|
10
- if (!i.is_a?(Array))
11
- raise ArgumentError.new("Specification item #{j} must be an array")
12
- end
13
- if (i.size()<2)
14
- raise ArgumentError.new("Specification item #{j} is too small, must contain at least 2 items")
15
- end
16
- if (i.size()>3)
17
- raise ArgumentError.new("Specification item #{j} is too largw, must contain no more than 3 items")
18
- end
19
- }
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
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
- def [](key)
28
- @spec_map[key]
29
- end
30
-
31
- def include?(x)
32
- @spec_map.include?(x)
33
- end
27
+ def [](key)
28
+ @spec_map[key]
29
+ end
34
30
 
35
- def regex()
36
- @regex
37
- end
31
+ def include?(x)
32
+ @spec_map.include?(x)
33
+ end
38
34
 
39
- def tokens()
40
- @tokens
41
- end
42
- end
35
+ attr_reader :regex
36
+
37
+ attr_reader :tokens
38
+ end
43
39
  end
@@ -1,37 +1,31 @@
1
1
  require 'set'
2
2
  class TokenData
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
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
- def substitute()
15
- @substitute
16
- end
17
-
18
- def start()
19
- @start
20
- end
14
+ attr_reader :substitute
21
15
 
22
- def finish()
23
- @finish
24
- end
16
+ attr_reader :start
25
17
 
26
- def substitute?()
27
- @isSub
28
- end
29
-
30
- def all()
31
- if (substitute?())
32
- [@substitute]
33
- else
34
- [@start,@finish]
35
- end
36
- end
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.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-07-10 00:00:00.000000000 Z
11
+ date: 2018-09-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: faissaloo@firemail.cc