Skip to content

Instantly share code, notes, and snippets.

@dustalov
Last active December 20, 2016 13:07
Show Gist options
  • Save dustalov/8c5ae1761fe311a86d46 to your computer and use it in GitHub Desktop.
Save dustalov/8c5ae1761fe311a86d46 to your computer and use it in GitHub Desktop.
Listing generator for a computer program copyright registration.
#!/usr/bin/env ruby
TEMPLATE = File.read('source.tex.erb').freeze
SOURCE = File.expand_path(ENV['SOURCE'] || '.').freeze
EXTENSIONS = %w(*.java).freeze
IGNORE = [
/package-info\.java$/,
/^src\/test/,
/^target/,
/^dependency-reduced-pom.xml/
].freeze
require 'erb'
Source = Struct.new(:filename, :path)
File.open('source.tex', 'w') do |file|
EXTENSIONS.each do |extension|
Dir['%s/**/%s' % [SOURCE, extension]].sort.each do |path|
filename = path[SOURCE.length + 1..-1]
next if IGNORE.any? { |re| re =~ filename }
source = Source.new(filename, path)
result = ERB.new(TEMPLATE).result(source.instance_eval { binding })
file.puts(result)
file.puts
end
end
end
all: clean lister latex view
clean:
latexmk -C -pdf
rm -f source.tex
lister:
./lister.rb
latex:
latexmk -pdf -pdflatex="xelatex %O %S" listing
view:
xdg-open listing.pdf
\subsection*{<%= filename.gsub('_', '\_') %>}
<% if /\.java$/ =~ filename %>
\lstinputlisting[language=java,firstline=17]{<%= path %>}
<% else %>
\lstinputlisting{<%= path %>}
<% end %>
\thispagestyle{empty}
\centering
{\addtolength{\parskip}{-\parskip}
\vspace*{10em}
\uppercase{Программа для ЭВМ}
\bigskip
{\Large\bfseries%
Название программы для ЭВМ%
}
\bigskip
Фрагменты исходного текста программы
\bigskip
\begin{flushright}
\begin{minipage}{.5\textwidth}
Листов \pageref{LastPage}.
\end{minipage}
\end{flushright}
\bigskip
\begin{flushright}
\begin{minipage}{.5\textwidth}
Автор:
Фамилия Имя Отчество.
\end{minipage}
\end{flushright}
\vfill
\textcopyright~Фамилия Имя Отчество, 2016.
\vspace{3em}
г.~Екатеринбург\\
2016~г.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment