Skip to content

Commit

Permalink
Added support for subsections
Browse files Browse the repository at this point in the history
  • Loading branch information
spinatelli committed Jun 17, 2013
1 parent 1ee42cd commit 9052459
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ v 1.0
======
Hooray! v 1.0 is out! Here below you can find its basic (and so far also complete) usage:

$ ruby rlatex.rb new --author "Edoardo Colombo" --title "My ruby latex manager" --sections my_fist_section foo
$ ruby rlatex.rb new --author "Edoardo Colombo" --title "My ruby latex manager" --sections my_fist_section/subsection1,subsection2 foo

The above command will produce the following:

1. a file called `main.tex` with:
* author set to Edoardo Colombo
* title set to My ruby latex manager
2. a folder named `contents` with inside a file named `my_first_section.tex`
2. a folder named `contents` with inside a file named `my_first_section.tex` and a file named `foo.tex`
3. a folder inside `contents`, named `my_first_section` for its subsections
4. two files for the two subsections in `my_first_section.tex`, `subsection1.tex` and `subsection2.tex`

Other options are:

Expand Down
57 changes: 54 additions & 3 deletions rlatex.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
require 'rubygems'
gem 'trollop'
gem 'facets'

Expand All @@ -17,14 +18,16 @@ def new_project(name, author, title, date, font_size, language, dclass, sections
@language = language
@font_size = font_size
@class = dclass
@sections = sections
@sectionsstring = sections
@sections = Array.new

FileUtils.mkdir name
FileUtils.mkdir "#{name}/contents"
FileUtils.mkdir "#{name}/pictures"

create_main_tex()
create_sections()
create_subsections()
else
puts "Directory #{name} already exists. Please provide another name."
end
Expand All @@ -45,20 +48,68 @@ def create_main_tex()
f.puts "\\begin{document}\n"
f.puts "\\maketitle"
f.puts " %%% INPUT"
@sections.each { |s| f.puts " \\input{contents/#{s}.tex}" } unless @sections.nil?

@sectionsstring.each { |s| parse_section(s, f) } unless @sectionsstring.nil?

f.puts("\\end{document}\n")
end
end

def parse_section(s, f)
sectionname = s.split("/")[0]
subsecsarray = s.split("/")[1]

@sections.push(sectionname)

unless subsecsarray.nil?
splitted = subsecsarray.split(",")

subsecs = splitted.map { |el|
{:SecName => sectionname, :SubSec => el }
}
if @subsectionsarray.nil?
@subsectionsarray = subsecs
else
@subsectionsarray = @subsectionsarray + subsecs
end
end

f.puts " \\input{contents/#{sectionname}.tex}"
end

def create_sections()
@sections.each { |section| create_section section } unless @sections.nil?
end

def create_section(section)
puts "creating section #{@name}/contents/#{section}.tex"
File.open("#{@name}/contents/#{section}.tex", 'w') do |f|
f.puts "\\section{#{section.tr('_',' ').titlecase}}\n"
f.puts "\\label{sec:#{section}}\n"

f.puts " %%% INPUT"
subsecs = @subsectionsarray.select{|s| s[:SecName]==section}
subsecs.each { |subsec| f.puts " \\input{contents/#{subsec[:SubSec]}.tex}" } unless subsecs.nil?

end
end

def create_subsections()
@subsectionsarray.each { |subsection| create_subsection subsection } unless @subsectionsarray.nil?
end

def create_subsection(subsection)
subdir = "#{@name}/contents/#{subsection[:SecName]}"

if not File.exists?(subdir)
FileUtils.mkdir subdir
end

puts "creating subsection #{subdir}/#{subsection[:SubSec]}.tex"

File.open("#{subdir}/#{subsection[:SubSec]}.tex", 'w') do |f|
f.puts "\\subsection{#{subsection[:SubSec].tr('_',' ').titlecase}}\n"
f.puts "\\label{sec:#{subsection[:SubSec]}}\n"
end
end

Expand Down Expand Up @@ -114,4 +165,4 @@ def write_packages(file)
o[:language],
o[:class],
o[:sections])
end
end

0 comments on commit 9052459

Please sign in to comment.