From 9052459bdc7edf9455b81ff6883d6ddd281471b2 Mon Sep 17 00:00:00 2001 From: Sergio Spinatelli Date: Mon, 17 Jun 2013 02:21:07 +0200 Subject: [PATCH] Added support for subsections --- README.md | 6 ++++-- rlatex.rb | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 90d92f0..d399f59 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/rlatex.rb b/rlatex.rb index 3e43e6d..424c2e5 100644 --- a/rlatex.rb +++ b/rlatex.rb @@ -1,3 +1,4 @@ +require 'rubygems' gem 'trollop' gem 'facets' @@ -17,7 +18,8 @@ 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" @@ -25,6 +27,7 @@ def new_project(name, author, title, date, font_size, language, dclass, sections create_main_tex() create_sections() + create_subsections() else puts "Directory #{name} already exists. Please provide another name." end @@ -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 @@ -114,4 +165,4 @@ def write_packages(file) o[:language], o[:class], o[:sections]) -end \ No newline at end of file +end