to_latex 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -18,7 +18,11 @@ Jeweler::Tasks.new do |gem|
18
18
  gem.homepage = "http://github.com/cie/to_latex"
19
19
  gem.license = "MIT"
20
20
  gem.summary = %Q{Adds #to_latex to objects and strings to convert them to LaTeX.}
21
- gem.description = %Q{Adds #to_latex to objects and strings.}
21
+ gem.description = %Q{Adds #to_latex to objects and strings. Works like html_safe in Rails 3, it escapes special characters as needed. Once converted, a string becomes a ToLatex::LatexString instance, with #latex? returning true, and never again will be escaped.
22
+
23
+ You can concatenate any escaped or not escaped string to an escaped one, they get properly escaped.
24
+
25
+ This gem powers texerb.}
22
26
  gem.email = "kallo.bernat@gmail.com"
23
27
  gem.authors = ["Bernát Kalló"]
24
28
  # dependencies defined in Gemfile
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.3.0
@@ -1,11 +1,15 @@
1
- class String
2
-
1
+ module ToLatex
3
2
  LATEX_SPECIAL_CHAR = /[\\{}$&\n#^_%~]/
4
3
 
5
- def to_latex!
6
- return self if latex?
4
+ DELIMITER_PAIRS = {
5
+ "$" => "$",
6
+ "$$" => "$$",
7
+ "\\(" => "\\)",
8
+ "\\[" => "\\]"
9
+ }
7
10
 
8
- gsub!(LATEX_SPECIAL_CHAR) do |c|
11
+ def self.escape s
12
+ s.gsub(LATEX_SPECIAL_CHAR) do |c|
9
13
  case c
10
14
  when "\\" then '\backslash'
11
15
  when "^" then '\char94'
@@ -13,41 +17,73 @@ class String
13
17
  else "\\#{c}"
14
18
  end
15
19
  end
16
-
17
- self.latex!
18
20
  end
19
21
 
22
+ # A string converted to latex
23
+ class LatexString < String
24
+ def to_latex
25
+ self
26
+ end
20
27
 
21
- def to_latex
22
- return self if latex?
28
+ def latex!
29
+ self
30
+ end
23
31
 
24
- dup.to_latex!
25
- end
32
+ def latex?
33
+ true
34
+ end
35
+
36
+ def concat value
37
+ if value.latex?
38
+ super value
39
+ else
40
+ super value.to_latex
41
+ end
42
+ end
43
+
44
+ alias << concat
26
45
 
27
- def to_latex_math
28
- "$#{to_latex}$"
46
+ def +(other)
47
+ dup.concat(other)
48
+ end
49
+
50
+ def to_s
51
+ self
52
+ end
29
53
  end
30
54
 
55
+ end
31
56
 
32
- def latex?
33
- defined?(@is_latex) && @is_latex
57
+ class String
58
+ # Convert a string to latex with escaping special characters.
59
+ def to_latex
60
+ ToLatex::LatexString.new ToLatex.escape(self)
34
61
  end
35
62
 
63
+ # Convert a string to latex without escaping. Despite the bang, this is NOT
64
+ # destructive, it returns a new object.
36
65
  def latex!
37
- @is_latex = true
38
- self
66
+ ToLatex::LatexString.new self
39
67
  end
40
68
 
69
+ # Tells if this is a string converted to latex
70
+ def latex?
71
+ false
72
+ end
41
73
  end
42
74
 
43
75
 
44
76
 
45
77
  class Object
78
+ # Convert an object to latex with escaping
46
79
  def to_latex
47
- to_s.to_latex!
80
+ to_s.to_latex
48
81
  end
49
82
 
50
- def to_latex_math
51
- "$#{to_latex}$".latex!
83
+ # Convert an object to latex with escaping and adding $ $ or other delimiter around
84
+ def to_latex_math open = "$", close = nil
85
+ close ||= ToLatex::DELIMITER_PAIRS[open] || open
86
+ ToLatex::LatexString.new "#{open}#{to_latex}#{close}"
52
87
  end
53
88
  end
89
+
@@ -1,7 +1,7 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe "ToLatex" do
4
- specify "String" do
4
+ specify "String#to_latex" do
5
5
  '%'.to_latex.should == '\%'
6
6
  '!'.to_latex.should == '!'
7
7
  '^'.to_latex.should == '\char94'
@@ -12,19 +12,89 @@ describe "ToLatex" do
12
12
 
13
13
  a = 'a'
14
14
  a.should_not be_latex
15
- a.to_latex!.should be_equal a
16
- a.should be_latex
15
+ a.to_latex.should_not be_equal a
16
+ a.to_latex.should be_latex
17
+ end
18
+
19
+ specify "String#latex!" do
20
+ '%'.latex!.should == '%'
21
+ '!'.latex!.should == '!'
22
+ '^'.latex!.should == '^'
23
+ 'a'.latex!.should == 'a'
24
+
25
+ 'a'.latex!.should be_latex
26
+
27
+ a = 'a'
28
+ a.should_not be_latex
29
+ a.latex!.should_not be_equal a
30
+ a.latex!.should be_latex
31
+
32
+ b = a.to_latex
33
+ b.latex!.should be_equal b
34
+ end
35
+
36
+ specify "String#latex! is not destructive" do
37
+ "a%!\\#".freeze.latex!
38
+ end
17
39
 
40
+ specify "String#to_latex is not destructive" do
41
+ "a%!\\#".freeze.to_latex
18
42
  end
19
43
 
44
+
45
+
20
46
  specify "Integer" do
21
47
  5.to_latex.should == "5"
22
48
  end
23
49
 
24
50
  specify "to_latex_math" do
25
51
  5.to_latex_math.should == "$5$"
52
+ 5.to_s.to_latex_math.should == "$5$"
53
+ 5.to_s.to_latex.to_latex_math.should == "$5$"
54
+ 5.to_s.to_latex.to_latex_math.to_latex.should == "$5$"
26
55
  "a".to_latex_math.should == "$a$"
56
+ "a".to_s.to_latex_math.should == "$a$"
57
+ "a".to_s.to_latex.to_latex_math.should == "$a$"
58
+ "a".to_s.to_latex.to_latex_math.to_latex.should == "$a$"
59
+ "5%".to_latex_math.should == "$5\\%$"
60
+ "5%".to_s.to_latex_math.should == "$5\\%$"
61
+ "5%".to_s.to_latex.to_latex_math.should == "$5\\%$"
62
+ "5%".to_s.to_latex.to_latex_math.to_latex.should == "$5\\%$"
63
+
64
+ "5%".to_latex_math("\\[").should == "\\[5\\%\\]"
65
+ "5%".to_latex_math("\\(").should == "\\(5\\%\\)"
66
+ "5%".to_latex_math("$").should == "$5\\%$"
67
+ "5%".to_latex_math("$$").should == "$$5\\%$$"
68
+ "5%".to_latex_math("a").should == "a5\\%a"
69
+ "5%".to_latex_math("a","b").should == "a5\\%b"
27
70
  end
28
71
 
72
+ specify "dup" do
73
+ "a".dup.should_not be_latex
74
+ "a".to_latex.dup.should be_latex
75
+ "a".to_latex.dup.should == "a"
76
+ end
77
+
78
+ specify "concat" do
79
+ ("a" + "#").should == "a#"
80
+ ("a".concat "#").should == "a#"
81
+ ("a" << "#").should == "a#"
82
+ ("a".to_latex + "#").should == "a\\#"
83
+ ("a".to_latex.concat "#").should == "a\\#"
84
+ ("a".to_latex << "#").should == "a\\#"
85
+ ("a".to_latex + "#".to_latex).should == "a\\#"
86
+ ("a".to_latex.concat "#".to_latex).should == "a\\#"
87
+ ("a".to_latex << "#".to_latex).should == "a\\#"
88
+ ["a\\".to_latex]
89
+ end
90
+
91
+ specify "to_s[tr]" do
92
+ a="a".to_latex
93
+ a.to_s.should == "a"
94
+ a.to_s.should be_latex
95
+ a.to_str.should == "a"
96
+ a.to_str.should_not be_latex
97
+ end
98
+
29
99
 
30
100
  end
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "to_latex"
8
- s.version = "0.1.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Bern\u{e1}t Kall\u{f3}"]
12
- s.date = "2012-03-13"
13
- s.description = "Adds #to_latex to objects and strings."
12
+ s.date = "2012-03-15"
13
+ s.description = "Adds #to_latex to objects and strings. Works like html_safe in Rails 3, it escapes special characters as needed. Once converted, a string becomes a ToLatex::LatexString instance, with #latex? returning true, and never again will be escaped.\n \n You can concatenate any escaped or not escaped string to an escaped one, they get properly escaped.\n \n This gem powers texerb."
14
14
  s.email = "kallo.bernat@gmail.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_latex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-13 00:00:00.000000000Z
12
+ date: 2012-03-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &79518630 !ruby/object:Gem::Requirement
16
+ requirement: &75258230 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.8.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *79518630
24
+ version_requirements: *75258230
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rdoc
27
- requirement: &79518340 !ruby/object:Gem::Requirement
27
+ requirement: &75257890 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.12'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *79518340
35
+ version_requirements: *75257890
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: bundler
38
- requirement: &79518020 !ruby/object:Gem::Requirement
38
+ requirement: &75257580 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 1.0.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *79518020
46
+ version_requirements: *75257580
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: jeweler
49
- requirement: &79517720 !ruby/object:Gem::Requirement
49
+ requirement: &75257280 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.8.3
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *79517720
57
+ version_requirements: *75257280
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rcov
60
- requirement: &79517360 !ruby/object:Gem::Requirement
60
+ requirement: &75256940 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,8 +65,12 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *79517360
69
- description: ! 'Adds #to_latex to objects and strings.'
68
+ version_requirements: *75256940
69
+ description: ! "Adds #to_latex to objects and strings. Works like html_safe in Rails
70
+ 3, it escapes special characters as needed. Once converted, a string becomes a ToLatex::LatexString
71
+ instance, with #latex? returning true, and never again will be escaped.\n \n
72
+ \ You can concatenate any escaped or not escaped string to an escaped one, they
73
+ get properly escaped.\n \n This gem powers texerb."
70
74
  email: kallo.bernat@gmail.com
71
75
  executables: []
72
76
  extensions: []
@@ -101,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
101
105
  version: '0'
102
106
  segments:
103
107
  - 0
104
- hash: 907125359
108
+ hash: 172590191
105
109
  required_rubygems_version: !ruby/object:Gem::Requirement
106
110
  none: false
107
111
  requirements: