rulex 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b837fec66c1b803f2637a4a8158437c68cb463cd
4
+ data.tar.gz: 85fa266b72812926f6a4f1e4b1f73c674e44636c
5
+ SHA512:
6
+ metadata.gz: dc75afa077337de174796be17e7f34fb07b633cda9a7ee9aa19f0021956db8e5134e837cb23399b634a4b45b327a435eb1c08de2cebb90298a57b20fbc4f6e2a
7
+ data.tar.gz: 65bec4fdc03d4869418162d50380595ac45705d346bff3868b46b1a9cdb2032c29050ad0f047ed2161ab25e1506b44df349b1dd1a5a152b35211b72f722bb90c
data/.gitignore ADDED
@@ -0,0 +1,37 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+ ## Specific to RubyMotion:
12
+ .dat*
13
+ .repl_history
14
+ build/
15
+ ## Documentation cache and generated files:
16
+ /.yardoc/
17
+ /_yardoc/
18
+ /doc/
19
+ /rdoc/
20
+ ## Environment normalisation:
21
+ /.bundle/
22
+ /vendor/bundle
23
+ /lib/bundler/man/
24
+ # for a library or gem, you might want to ignore these files since the code is
25
+ # intended to run in multiple environments; otherwise, check them in:
26
+ .ruby-version
27
+ .ruby-gemset
28
+ Gemfile.lock
29
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
30
+ .rvmrc
31
+
32
+
33
+ # PDFs might be generated
34
+ /**/*.pdf
35
+
36
+ # Vim's crap
37
+ /**/*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rulex.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # Rulex
2
+
3
+ Rulex is a rubygem allowing you to use Ruby while writing LaTex files. It reads rulex `.rex` files, and converts them into LaTex `.tex` files. A `.rex` file is a Ruby file; you can use (almost) everything the way you would in a Ruby script.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rulex'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rulex
20
+
21
+ ## Usage
22
+
23
+ **Everything is still very experimental, and might break at any point!**
24
+
25
+ *For detailed documentation, please refer to the code, mainly `/spec/rulex_spec.rb`. There are a few examples as well in `/examples/`.*
26
+
27
+
28
+ ### Example
29
+
30
+ ```ruby
31
+ # example.rex
32
+ documentclass :article
33
+
34
+ def count_from range
35
+ first = range.first
36
+ last = range.last
37
+
38
+ subsection "how to count from #{first} to #{last}"
39
+ raw "Let's try to count."
40
+
41
+ itemize do
42
+ range.each do |n|
43
+ item n.to_s
44
+ end
45
+ end
46
+ end
47
+ document do
48
+ section "A Short Lecture on How to Count"
49
+
50
+ count_from (1..5)
51
+ count_from (10..20)
52
+
53
+ raw "Good job, now off to section "; ref :acks;
54
+
55
+ section "Acknowledgements"
56
+ label :acks
57
+
58
+ raw "Finally I would like to thank \n"
59
+
60
+ enumerate do
61
+ %w[Donald\ Knuth Yukihiro\ Matsumoto].each do |name|
62
+ item "Mr. #{name}"
63
+ end
64
+ end
65
+ end
66
+ ```
67
+
68
+ Run `rulex example.rex > example.tex` to get
69
+
70
+ ```latex
71
+ % example.tex
72
+ \documentclass{article}
73
+ \begin{document}
74
+ \section{A Short Lecture on How to Count}
75
+ \subsection{how to count from 1 to 5}
76
+ Let's try to count.\begin{itemize}
77
+ \item{1}
78
+ \item{2}
79
+ \item{3}
80
+ \item{4}
81
+ \item{5}
82
+ \end{itemize}
83
+ \subsection{how to count from 10 to 20}
84
+ Let's try to count.\begin{itemize}
85
+ \item{10}
86
+ \item{11}
87
+ \item{12}
88
+ \item{13}
89
+ \item{14}
90
+ \item{15}
91
+ \item{16}
92
+ \item{17}
93
+ \item{18}
94
+ \item{19}
95
+ \item{20}
96
+ \end{itemize}
97
+ Good job, now off to section \ref{acks}
98
+ \section{Acknowledgements}
99
+ \label{acks}
100
+ Finally I would like to thank
101
+ \begin{enumerate}
102
+ \item{Mr. Donald Knuth}
103
+ \item{Mr. Yukihiro Matsumoto}
104
+ \end{enumerate}
105
+ \end{document}
106
+ ```
107
+
108
+ ## Development
109
+
110
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
111
+
112
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
113
+
114
+ ## Contributing
115
+
116
+ 1. Fork it ( https://github.com/Nicowcow/rulex/fork )
117
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
118
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
119
+ 4. Push to the branch (`git push origin my-new-feature`)
120
+ 5. Create a new Pull Request
121
+
122
+
123
+ ### What can I do?
124
+
125
+ * `Rulex::Tex::Grammar::Document` is a big misnommer! It should be changed to something that reflects better what its role is.
126
+ * `Rulex::Rex::Reader#raw` might not be the best name either. Maybe change it to text.
127
+ * Add markdown support to `Rulex::Rex::Reader` through Pandoc
128
+ * The parser needs some beefin-up (not sure what it does with spaces, especially with command options)
129
+ * Maybe add some syntax for LaTeX commands to output text directly rather than store the command in the tree
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rulex"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,13 @@
1
+ \documentclass{article}
2
+
3
+ \begin{document}
4
+
5
+ Hello, { bracket bloc! }
6
+
7
+
8
+ Hello, math env!$\frac{1}{34}$
9
+
10
+
11
+ Hello, space between arg blocs!$\frac{1} {34}$
12
+
13
+ \end{document}
@@ -0,0 +1,4 @@
1
+ documentclass :article
2
+ document do
3
+ raw "Hello, World!\n"
4
+ end
@@ -0,0 +1,4 @@
1
+ \documentclass{article}
2
+ \begin{document}
3
+ Hello, World!
4
+ \end{document}
@@ -0,0 +1,4 @@
1
+ \documentclass{article}
2
+ \begin{document}
3
+ Hello, world!
4
+ \end{document}
data/exe/rulex ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rulex"
4
+
5
+
6
+ rex_reader = Rulex::Rex::Reader.new
7
+ tex_writer = Rulex::Tex::Writer.new
8
+
9
+ rex_reader.read File.open(ARGV[0]).read
10
+ tex_writer.import rex_reader.export
11
+ puts tex_writer.export
@@ -0,0 +1,61 @@
1
+ module Rulex
2
+ module Rex
3
+ class Reader
4
+ def initialize
5
+ @content = []
6
+ @content_stack = [@content]
7
+ @latex_reader = Rulex::Tex::Reader.new
8
+ end
9
+
10
+ alias_method :read, :instance_eval
11
+
12
+ def add_to_content node
13
+ @content_stack.last << node
14
+ end
15
+
16
+ def raw str
17
+ add_to_content(type: :text, text: str)
18
+ #new_node = {type: :text, text: str }
19
+ #@content << new_node
20
+ end
21
+
22
+ def tex str
23
+ add_to_content(type: :tex, children: @latex_reader.read(str))
24
+ end
25
+
26
+ def import filepath
27
+ read File.open(filepath).read
28
+ end
29
+
30
+ def export
31
+ @content
32
+ end
33
+
34
+ def tex_command(name, args)
35
+ add_to_content(type: :command, name: name, arguments: args)
36
+ end
37
+
38
+ def tex_environment(name, args, block)
39
+ new_node = {type: :environment, name: name, arguments: args}
40
+ @content_stack.push []
41
+ read &block
42
+ new_node.merge!(children: @content_stack.pop)
43
+
44
+ #new_node = {type: :environment, name: name, arguments: args}
45
+ #deeper_parser = Rulex::Rex::Reader.new
46
+ #deeper_parser.read &block
47
+ #new_node.merge!(children: deeper_parser.export)
48
+
49
+ add_to_content new_node
50
+ end
51
+
52
+ def method_missing(m_id, *args, &block)
53
+ if block
54
+ tex_environment(m_id, args, block)
55
+ else
56
+ tex_command(m_id, args)
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,53 @@
1
+ grammar Latex
2
+ rule document
3
+ (environment/command/text)* <Rulex::Tex::Grammar::Document>
4
+ end
5
+
6
+ rule environment
7
+ "\\begin{" environment_name "}" environment_text "\\end{" environment_name &{ |seq| seq[1].text_value == seq[5].text_value } "}" <Rulex::Tex::Grammar::Environment>
8
+ end
9
+
10
+ rule environment_text
11
+ #document &{ |s| puts s[0].text_value; true}
12
+ #( document) !"\\end{" &{ |s| puts s[0].text_value; true}
13
+ document
14
+
15
+ #[^\\]* &{ |s| puts s[0].text_value; true}
16
+ #[^\\]*
17
+ end
18
+
19
+
20
+ rule environment_name
21
+ [^}\s]+
22
+ end
23
+
24
+ rule command
25
+ "\\" command_name ('[' command_option (',' command_option)* ']')? ('{' command_argument '}')* <Rulex::Tex::Grammar::Command>
26
+ end
27
+
28
+ rule command_option
29
+ (![\s,] [a-z0-9])+ # With the neg. lookahead we make sure we don't consume the next command
30
+ end
31
+
32
+ rule command_name
33
+ !"begin" # First, we make sure it is not the beginning of an environment
34
+ (!"end" # Then, we make sure it won't consume the end of an environment
35
+ ([^\[{\s]+)) # Finally, we don't want to consume the options or args of the command
36
+ end
37
+
38
+ rule command_argument
39
+ [^}]+
40
+ end
41
+
42
+ rule text
43
+ (spaces? word spaces?)+ <Rulex::Tex::Grammar::Text>
44
+ end
45
+
46
+ rule word
47
+ ([^\s\\])+
48
+ end
49
+
50
+ rule spaces
51
+ [\s\n]+
52
+ end
53
+ end
@@ -0,0 +1,60 @@
1
+ module Rulex
2
+ module Tex
3
+ module Grammar
4
+
5
+ class Treetop::Runtime::SyntaxNode
6
+ def node_content
7
+ h = {type: :node} #, log: elements.to_s}
8
+ h.merge!(:children => elements.map{|e| e.node_content}) if elements && !elements.empty?
9
+ h.merge!(log: text_value)
10
+ h.merge! content
11
+ end
12
+ def content
13
+ {}
14
+ end
15
+ end
16
+
17
+ class CustomNode < Treetop::Runtime::SyntaxNode
18
+ end
19
+
20
+ class Document < CustomNode
21
+ def content
22
+ {type: :document}
23
+ end
24
+ end
25
+
26
+ class Text < CustomNode
27
+ def content
28
+ {type: :text, text: text_value}
29
+ end
30
+ end
31
+
32
+ class Command < CustomNode
33
+ def content
34
+ h = {type: :command, name: elements[1].text_value}
35
+ if opts_maybe = elements[2].elements
36
+ opts = []
37
+ opts << opts_maybe[1].text_value
38
+
39
+ if opts_kleene_s = opts_maybe[2].elements
40
+ opts += opts_kleene_s.map{|e| e.elements[1].text_value}
41
+ end
42
+
43
+ h.merge!(options: opts)
44
+ end
45
+ if args_kleene_s = elements[3]
46
+ h.merge!(arguments: args_kleene_s.elements.map{|e| e.elements[1].text_value})
47
+ end
48
+
49
+ h
50
+ end
51
+ end
52
+
53
+ class Environment < CustomNode
54
+ def content
55
+ {type: :environment, name: elements[1].text_value, children: elements[3].elements.map{|e| e.content }}
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,29 @@
1
+ require 'treetop'
2
+ require_relative 'node_extensions'
3
+
4
+ grammar_path = File.join(File.dirname(File.expand_path(__FILE__)),
5
+ 'latex.treetop')
6
+ Treetop.load grammar_path
7
+
8
+
9
+ module Rulex
10
+ module Tex
11
+ class Reader
12
+
13
+ def initialize
14
+ @parser = LatexParser.new
15
+ @content = []
16
+ end
17
+
18
+
19
+ def to_a
20
+ @content
21
+ end
22
+ alias_method :export, :to_a
23
+
24
+ def read str
25
+ @content = @parser.parse(str).node_content
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,38 @@
1
+ module Rulex
2
+ module Tex
3
+ class Writer
4
+
5
+ def initialize
6
+ @content = ''
7
+ end
8
+
9
+ def import arr
10
+ return unless arr
11
+ arr.each do |item|
12
+
13
+ case item[:type]
14
+ when :command
15
+ @content += "\\#{item[:name]}"
16
+ item[:arguments].each do |arg|
17
+ @content += "{#{arg}}"
18
+ end
19
+ @content += "\n"
20
+ when :text
21
+ @content += item[:text]
22
+ when :environment
23
+ @content += "\\begin{#{item[:name]}}\n"
24
+ import item[:children]
25
+ @content += "\\end{#{item[:name]}}\n"
26
+ else
27
+ import item[:children] if item[:children]
28
+ end
29
+ end
30
+ end
31
+
32
+ def to_s
33
+ @content
34
+ end
35
+ alias_method :export, :to_s
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module Rulex
2
+ VERSION = "0.1.0"
3
+ end
data/lib/rulex.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "rulex/version"
2
+ require "rulex/tex/reader"
3
+ require "rulex/tex/writer"
4
+ require "rulex/rex/reader"
5
+
6
+ module Rulex
7
+ end
data/rulex.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rulex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rulex"
8
+ spec.version = Rulex::VERSION
9
+ spec.authors = ["Nicolas Mattia"]
10
+ spec.email = ["nicowcowo@gmail.com"]
11
+
12
+ spec.summary = %q{A gem for easily wrapping LaTeX code in Ruby}
13
+ spec.homepage = "https://www.github.com/Nicowcow/rulex"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ #if spec.respond_to?(:metadata)
21
+ #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
22
+ #end
23
+
24
+ spec.add_runtime_dependency "treetop"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.9"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec"
29
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rulex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Mattia
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: treetop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - nicowcowo@gmail.com
72
+ executables:
73
+ - rulex
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - examples/bracket_bloc.tex
86
+ - examples/hello_world.rex
87
+ - examples/hello_world.tex
88
+ - examples/simple.tex
89
+ - exe/rulex
90
+ - lib/rulex.rb
91
+ - lib/rulex/rex/reader.rb
92
+ - lib/rulex/tex/latex.treetop
93
+ - lib/rulex/tex/node_extensions.rb
94
+ - lib/rulex/tex/reader.rb
95
+ - lib/rulex/tex/writer.rb
96
+ - lib/rulex/version.rb
97
+ - rulex.gemspec
98
+ homepage: https://www.github.com/Nicowcow/rulex
99
+ licenses: []
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.4.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: A gem for easily wrapping LaTeX code in Ruby
121
+ test_files: []