rulex 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -25
- data/examples/count.rex +6 -1
- data/lib/rulex/rex/reader.rb +24 -9
- data/lib/rulex/tex/writer.rb +6 -2
- data/lib/rulex/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 615cc7d413292fbe99bd79cb0cdaee48d9e6ca91
|
4
|
+
data.tar.gz: 32dfee7e649b5f48c60d1f5e0e2d59e37c01b382
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d506ff5ad0e7037afa8498884f06b4c591de5074b5de206480b21ca90b2e1db5011e36d517993eeb3f89334861e0918e6e0bc184c818b0d9eeda35a34107ccee
|
7
|
+
data.tar.gz: d4f609a790eb152cc056ec6fb26a0bdbb27711e719616dd6d003f5b629689510188f152348d0fbede1513714fcd259ffcda0c4644395ca7c5b02aca46c9c7fc8
|
data/README.md
CHANGED
@@ -4,7 +4,16 @@ Rulex is a rubygem allowing you to use Ruby while writing LaTex files. It reads
|
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
|
-
|
7
|
+
To start using Rulex, you'll first need Ruby and Rubygems (here for a Debian-like, though I recommend rvm):
|
8
|
+
|
9
|
+
$ apt-get install ruby && apt-get install rubygems
|
10
|
+
|
11
|
+
and then install the gem as
|
12
|
+
|
13
|
+
$ gem install rulex
|
14
|
+
|
15
|
+
|
16
|
+
To use it in your own project, add this line to your application's Gemfile:
|
8
17
|
|
9
18
|
```ruby
|
10
19
|
gem 'rulex'
|
@@ -14,9 +23,6 @@ And then execute:
|
|
14
23
|
|
15
24
|
$ bundle
|
16
25
|
|
17
|
-
Or install it yourself as:
|
18
|
-
|
19
|
-
$ gem install rulex
|
20
26
|
|
21
27
|
## Usage
|
22
28
|
|
@@ -29,14 +35,11 @@ Or install it yourself as:
|
|
29
35
|
### Example
|
30
36
|
|
31
37
|
```ruby
|
32
|
-
# First thing to note, your rex file must follow the TeX document structure.
|
33
|
-
#
|
34
|
-
# `document` environment by calling `\begin{document}` and `\end{document}`.
|
35
|
-
# The only difference here is that most of this process is wrapped in Ruby.
|
38
|
+
# First thing to note, your rex file must follow the TeX document structure. You will start
|
39
|
+
# with a `\documentclass` call, then wrap your document in a `document` environment by calling `\begin{document}` and `\end{document}`. The only difference here is that most of this process is wrapped in Ruby.
|
36
40
|
|
37
41
|
|
38
|
-
# Just call `documentclass` to set the document class. This for instance will
|
39
|
-
# be translated to `\documentclass{article}`.
|
42
|
+
# Just call `documentclass` to set the document class. This for instance will be translated to `\documentclass{article}`.
|
40
43
|
documentclass :article
|
41
44
|
|
42
45
|
# You can define your own Ruby functions.
|
@@ -44,20 +47,14 @@ def count_from range
|
|
44
47
|
first = range.first
|
45
48
|
last = range.last
|
46
49
|
|
47
|
-
# Any function call, like `functionname("arg1", "arg2",...)`, will be
|
48
|
-
#
|
49
|
-
# a subsection as follows:
|
50
|
+
# Any function call, like `functionname("arg1", "arg2",...)`, will be translated to `\functionname{arg1}{arg2}{...}`.
|
51
|
+
# That way, you can start a subsection as follows:
|
50
52
|
subsection "how to count from #{first} to #{last}"
|
51
53
|
|
52
|
-
# Raw is a special `rex` function. It writes its argument as text in the rex
|
53
|
-
# tree (and subsequently in the TeX file) without parsing it. Note that the
|
54
|
-
# fact that it is not writing a `\raw` function is exceptional, because `raw`
|
55
|
-
# is a `rex` reserved function name. To use `raw` like you would use
|
56
|
-
# `subsection` call `tex_command :raw`.
|
54
|
+
# Raw is a special `rex` function. It writes its argument as text in the rex tree (and subsequently in the TeX file) without parsing it. Note that the fact that it is not writing a `\raw` function is exceptional, because `raw` is a `rex` reserved function name. To use `raw` like you would use `subsection` call `tex_command :raw`.
|
57
55
|
raw "Let's try to count."
|
58
56
|
|
59
|
-
# If you pass a command a bloc, it will start a TeX environment. The
|
60
|
-
# following `itemize do ... end` is equivalent to `\begin{itemize} ... \end{itemize}`.
|
57
|
+
# If you pass a command a bloc, it will start a TeX environment. The following `itemize do ... end` is equivalent to `\begin{itemize} ... \end{itemize}`.
|
61
58
|
itemize do
|
62
59
|
range.each do |n|
|
63
60
|
item n.to_s
|
@@ -69,13 +66,17 @@ document do
|
|
69
66
|
section "A Short Lecture on How to Count"
|
70
67
|
|
71
68
|
|
72
|
-
# You can of course call the functions you defined (AND NOT BE LIMITED TO
|
73
|
-
# 9 ******* ARGUMENTS)
|
69
|
+
# You can of course call the functions you defined (AND NOT BE LIMITED TO 9 ******* ARGUMENTS)
|
74
70
|
|
75
71
|
count_from (1..5)
|
76
72
|
count_from (10..20)
|
77
73
|
|
78
|
-
|
74
|
+
# At any time, you can prefix a method call with `pure_`. This will return the LaTeX text
|
75
|
+
# (`String`) that would have been produced, instead of writing the command to the
|
76
|
+
# document tree. For instance, we want to write the whole text "Good job, ... " to the
|
77
|
+
# document tree, while referencing the next section; we don't want to write the reference
|
78
|
+
# to the tree on top of that!
|
79
|
+
raw "Good job, now off to section #{pure_ref :acks}\n"
|
79
80
|
|
80
81
|
section "Acknowledgements"
|
81
82
|
label :acks
|
@@ -94,7 +95,6 @@ end
|
|
94
95
|
Run `rulex example.rex > example.tex` to get
|
95
96
|
|
96
97
|
```latex
|
97
|
-
% example.tex
|
98
98
|
\documentclass{article}
|
99
99
|
\begin{document}
|
100
100
|
\section{A Short Lecture on How to Count}
|
@@ -121,6 +121,7 @@ Let's try to count.\begin{itemize}
|
|
121
121
|
\item{20}
|
122
122
|
\end{itemize}
|
123
123
|
Good job, now off to section \ref{acks}
|
124
|
+
|
124
125
|
\section{Acknowledgements}
|
125
126
|
\label{acks}
|
126
127
|
Finally I would like to thank
|
@@ -139,13 +140,20 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
139
140
|
|
140
141
|
## Contributing
|
141
142
|
|
143
|
+
### I want no business with Ruby.
|
144
|
+
|
145
|
+
Rulex is very young, and everything is very likely to break down. Just use it, and open an issue if something goes wrong. You can also contact me.
|
146
|
+
|
147
|
+
### I want no business with LaTeX.
|
148
|
+
|
149
|
+
That's the point. Seriously, the code might need some refactoring, and can always use cool tricks.
|
150
|
+
|
142
151
|
1. Fork it ( https://github.com/Nicowcow/rulex/fork )
|
143
152
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
144
153
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
145
154
|
4. Push to the branch (`git push origin my-new-feature`)
|
146
155
|
5. Create a new Pull Request
|
147
156
|
|
148
|
-
|
149
157
|
### What can I do?
|
150
158
|
|
151
159
|
* `Rulex::Tex::Grammar::Document` is a big misnommer! It should be changed to something that reflects better what its role is.
|
@@ -154,3 +162,5 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
154
162
|
* The parser needs some beefin-up (not sure what it does with spaces, especially with command options)
|
155
163
|
* Maybe add some syntax for LaTeX commands to output text directly rather than store the command in the tree
|
156
164
|
* Coffee is on me if you write a Vim plugin
|
165
|
+
* Another coffee if you also write a Rake task.
|
166
|
+
* There should be some special delimiters like Ruby's `#{...}` (maybe `#< ... >`) which would translate all method calls to `pure_` method calls. That would make inlining method calls in strings much easier.
|
data/examples/count.rex
CHANGED
@@ -34,7 +34,12 @@ document do
|
|
34
34
|
count_from (1..5)
|
35
35
|
count_from (10..20)
|
36
36
|
|
37
|
-
|
37
|
+
# At any time, you can prefix a method call with `pure_`. This will return the LaTeX text
|
38
|
+
# (`String`) that would have been produced, instead of writing the command to the
|
39
|
+
# document tree. For instance, we want to write the whole text "Good job, ... " to the
|
40
|
+
# document tree, while referencing the next section; we don't want to write it to the
|
41
|
+
# tree on top of that!
|
42
|
+
raw "Good job, now off to section #{pure_ref :acks}\n"
|
38
43
|
|
39
44
|
section "Acknowledgements"
|
40
45
|
label :acks
|
data/lib/rulex/rex/reader.rb
CHANGED
@@ -8,18 +8,33 @@ module Rulex
|
|
8
8
|
@latex_reader = Rulex::Tex::Reader.new
|
9
9
|
end
|
10
10
|
|
11
|
-
|
11
|
+
def read *args, &block
|
12
|
+
if args.length == 1
|
13
|
+
read_rex args.first
|
14
|
+
elsif block
|
15
|
+
instance_eval &block
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def read_rex str
|
20
|
+
instance_eval rex_to_ruby str
|
21
|
+
end
|
12
22
|
|
13
|
-
|
23
|
+
# There are a few characters ('\\', '\[' and '\]') that %q[] escapes anyway
|
24
|
+
def rex_to_ruby str
|
25
|
+
str.gsub(/<##(((?!##>).)+)##>/) { |m| "raw %q[" + $1.gsub("\\","\\\\\\\\") + "]"}
|
26
|
+
end
|
27
|
+
|
28
|
+
def add_node_to_content node
|
14
29
|
@content_stack.last << node
|
15
30
|
end
|
16
31
|
|
17
32
|
def raw str
|
18
|
-
|
33
|
+
add_node_to_content(type: :text, text: str)
|
19
34
|
end
|
20
35
|
|
21
36
|
def tex str
|
22
|
-
|
37
|
+
add_node_to_content(type: :tex, children: @latex_reader.read(str))
|
23
38
|
end
|
24
39
|
|
25
40
|
def import filepath
|
@@ -50,31 +65,31 @@ module Rulex
|
|
50
65
|
end
|
51
66
|
|
52
67
|
def tex_command(name, params)
|
53
|
-
|
68
|
+
add_node_to_content build_tex_command name, params
|
54
69
|
end
|
55
70
|
|
56
71
|
def depth
|
57
72
|
@content_stack.length - 1
|
58
73
|
end
|
59
74
|
|
60
|
-
def tex_environment(name, args, block)
|
75
|
+
def tex_environment(name, *args, &block)
|
61
76
|
new_node = {type: :environment, name: name, arguments: args}
|
62
77
|
@content_stack.push []
|
63
78
|
read &block
|
64
79
|
new_node.merge!(children: @content_stack.pop)
|
65
|
-
|
80
|
+
add_node_to_content new_node
|
66
81
|
end
|
67
82
|
|
68
83
|
def method_missing(m_id, *args, &block)
|
69
84
|
if block
|
70
|
-
tex_environment(m_id, args, block)
|
85
|
+
tex_environment(m_id, *args, &block)
|
71
86
|
elsif /pure_([a-zA-Z]+)/.match(m_id)
|
72
87
|
Rulex::Tex::Writer.to_str(build_tex_command($1,args))
|
73
|
-
#"\\#{$1}{1}{2}"
|
74
88
|
else
|
75
89
|
tex_command(m_id, args)
|
76
90
|
end
|
77
91
|
end
|
92
|
+
|
78
93
|
end
|
79
94
|
end
|
80
95
|
end
|
data/lib/rulex/tex/writer.rb
CHANGED
@@ -26,8 +26,12 @@ module Rulex
|
|
26
26
|
end
|
27
27
|
res = str += "\\end{#{item[:name]}}\n"
|
28
28
|
else
|
29
|
-
|
30
|
-
|
29
|
+
if item[:children]
|
30
|
+
str = ""
|
31
|
+
res = str += item[:children].inject(""){|acc, c| acc += Rulex::Tex::Writer.to_str c} if item[:children] else
|
32
|
+
""
|
33
|
+
end
|
34
|
+
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
data/lib/rulex/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rulex
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Mattia
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: treetop
|