As part of my graduation research, write a Rakefile for creating a PDF used in the laboratory in the form of a runbook.
A program that clarifies the procedure and progresses while taking each step. You can write automation step by step as one of Ruby's DSL.
For a person like me who is a beginner and not yet accustomed to using CUI, executing Rakefile will proceed without permission and you will not know what you are doing. Therefore, by using the runbook, the procedure becomes clear and you can understand what you are doing. Also, with Rakefile, it is difficult if you do not write in the program from the beginning to stop even if you think something is wrong during execution, but with Runbook you can choose whether to continue asking the user for input each time in a finely divided procedure. You can.
The basic is a program consisting of a title, a section, and a step, and the program is written in the step. Since the runbook command has a template for starting, install it and check the basics of the runbook.
> runbook generate runbook my_first_runbook
This will generate my_first_runbook.rb in the directory.
my_first_runbook.rb
require "runbook"
runbook = Runbook.book "My First Runbook" do
description <<-DESC
This is a runbook that...
DESC
section "SECTION" do
step "STEP" do
# Add statements here
end
end
end
if __FILE__ == $0
Runbook::Runner.new(runbook).run
else
runbook
end
When you execute it, you will be asked if you want to continue as shown in the figure below.
> runbook exec my_first_runbook.rb
Executing My First Runbook...
Description:
This is a runbook that...
Section 1: SECTION
Step 1.1: STEP
Continue? (enter "h" for help) [c,s,j,P,e,h]
> runbook exec my_first_runbook.rb
Executing My First Runbook...
Description:
This is a runbook that...
Section 1: SECTION
Step 1.1: STEP
Continue? (enter "h" for help) [c,s,j,P,e,h] c
>> Continue to execute this step
> runbook exec my_first_runbook.rb
Executing My First Runbook...
Description:
This is a runbook that...
Section 1: SECTION
Step 1.1: STEP
Continue? Continue to execute this step
The structure of the Rakefile used in the laboratory is mainly --Reading an org file --Convert to latex --Changed latex to report format --Create PDF using platex
Since there are four, write a runbook with these as steps.
make_pdf.rb
Runbook.book "Make PDF" do
description <<-DESC
This is a make PDF from org
DESC
section "Make pdf" do
step "Load org file"
step "Make tex file"
step "Load and Convert tex file"
step "Make pdf"
end
end
Execution result
> runbook exec make_pdf.rb
Executing Make PDF...
Description:
This is a make PDF from org
Section 1: Make pdf
Step 1.1: Load org file
Continue? Continue to execute this step
Step 1.2: Make tex file
Continue? Continue to execute this step
Step 1.3: Load and Convert tex file
Continue? Continue to execute this step
Step 1.4: Make pdf
Now that we have a template, we will add the contents to it.
I thought it would be easier to stop by dividing the section into one that creates latex and one that converts to PDF, so do so and add the contents.
make_pdf.rb
require "./convert"
require "colorize"
Runbook.book "Make PDF" do
description <<-DESC
This is a make PDF from org
DESC
section "Make latex" do
$t_file = "report"
step "Load org file" do
note "Load org file"
ruby_command do
$file = Dir.glob("*.org")[0].match(/(.*).org/)[1]
puts "your org file is " + $file.red + "."
end
end
step "Make tex file" do
note "Make tex file"
ruby_command do
system "emacs #{$file}.org --batch -f org-latex-export-to-latex --kill"
end
end
step "Load and Convert tex file" do
ruby_command do
$lines = File.readlines("#{$file}.tex")
$lines = convert_thesis($lines)
File.open("#{$t_file}.tex", "w") do |f|
$lines.each { |line| f.print line }
end
end
end
section "Make PDF" do
step "Make pdf" do
note "Make pdf"
ruby_command do
commands = ["platex #{$t_file}.tex",
"bibtex #{$t_file}.tex",
"platex #{$t_file}.tex",
"dvipdfmx #{$t_file}.dvi"]
commands.each { |com| system com }
end
end
end
end
Now you have a runbook program that reads the org file in the directory with "Load org file", converts it to latex, and creates a PDF.
--convert.rb
is a program for using the function of convert_thesis
used in "Load and Convert tex file". The content is to convert latex created from org into a template for reporting.
convert.rb
def convert_thesis(lines)
head = <<'EOS'
\documentclass[a4j,twocolumn]{jsarticle}
\usepackage[dvipdfmx]{graphicx}
\usepackage{url}
\setlength{\textheight}{275mm}
\headheight 5mm
\topmargin -30mm
\textwidth 185mm
\oddsidemargin -15mm
\evensidemargin -15mm
\pagestyle{empty}
\begin{document}
\title{}
\author{Department\hspace{5mm}Student number\hspace{5mm} your name}
\date{}
\maketitle
EOS
head2 = <<'EOS'
{\small\setlength\baselineskip{15pt} %References are line spacing with smaller letters
\begin{thebibliography}{9}
\bibitem{}
\end{thebibliography}
}
\end{document}
EOS
new_line = [head]
lines[31..-1].each do |line|
new_line << line
end
new_line.each do |line|
line.gsub!('\end{document}', head2)
line.gsub!('\tableofcontents', "")
end
return new_line
end
--I want to be able to select when there are multiple org files. --The files (.aux and .div) created in the directory are hard to see even though you haven't changed them yourself, so please delete them or put them together.
Allows you to select org in "Load org file".
make_pdf.rb
require "./convert"
require "colorize"
Runbook.book "Make PDF" do
...
$t_file = "report"
step "Load org file" do
note "Load org file"
ruby_command do
str = Dir.glob("*.org")
str.each do |name|
puts "your org file is " + name.red + " ? (y or n)"
res = $stdin.gets.chomp
if res == "y"
$file = name.match(/(.*).org/)[1]
break
elsif res == "n"
if name == str[str.size - 1]
puts "This directory not have the objective file".red
exit
end
end
end
end
end
...
end
The name of the org file is displayed as a message on the terminal, and you can select it by entering "y" or "n". If all are "n", it will be forcibly terminated.
Next, at the very end, I made "Move report" and moved the created report to a directory called report.
make_pdf.rb
require "./convert"
require "colorize"
Runbook.book "Make PDF" do
...
section "Make PDF" do
step "Make pdf" do
...
step "Move report" do
note "Move report"
ruby_command do
commands = ["mkdir report",
"mv -f #{$t_file}.* ./report",
"open ./report/#{$t_file}.pdf"]
commands.each { |com| system com }
end
end
end
end
ββIt was pointed out that you should check if there is a way to do everything non-stop without inputting.
There was a description in the manual in Github of gem runbook.
> runbook exec --auto my_runbook.rb
If you execute it with this, you can execute it to the end without input.
Install with gem. gem install runbook
If you do runbook exec runbook.rb
, runbook.rb will be executed.
--runbook.rb is your program.
--Can be executed in any directory if you know the path of the program location
Recommended Posts