From 26351f4a11be4010d3403c7a7427f2cbace28eb0 Mon Sep 17 00:00:00 2001
From: Bruno Freitas Tissei <bft15@inf.ufpr.br>
Date: Mon, 18 Feb 2019 10:34:47 -0300
Subject: [PATCH] [WIP] Add new script to generate pdf

Signed-off-by: Bruno Freitas Tissei <bft15@inf.ufpr.br>
---
 alg2pdf                                    | 91 --------------------
 algorithms/graph/articulations_bridges.cpp |  2 +-
 algorithms/graph/bfs.cpp                   |  2 +-
 algorithms/graph/dfs.cpp                   |  2 +-
 algorithms/graph/lca.cpp                   |  6 +-
 algorithms/paradigm/kadane.cpp             |  2 +-
 algorithms/structure/ball_tree.cpp         | 12 +--
 notebook/gen_latex.py                      | 97 ++++++++++++++++++++++
 notebook/header.tex                        | 22 +++++
 run                                        | 10 +++
 10 files changed, 142 insertions(+), 104 deletions(-)
 delete mode 100755 alg2pdf
 create mode 100644 notebook/gen_latex.py
 create mode 100644 notebook/header.tex
 create mode 100755 run

diff --git a/alg2pdf b/alg2pdf
deleted file mode 100755
index 8548db0..0000000
--- a/alg2pdf
+++ /dev/null
@@ -1,91 +0,0 @@
-#!/bin/bash
-
-# Random temp file name
-tex_file=$(mktemp)
-
-# Print the tex file header
-cat<<EOF >$tex_file
-\documentclass{article}
-\usepackage{listings}
-\usepackage[usenames,dvipsnames]{color}  %% Allow color names
-\usepackage[letterpaper, portrait, margin=1in]{geometry}
-
-%\lstdefinestyle{customasm}{
-%  belowcaptionskip=1\baselineskip,
-%  xleftmargin=\parindent,
-%  language=C++,   %% Change this to whatever you write in
-%  breaklines=true, %% Wrap long lines
-%  basicstyle=\footnotesize\ttfamily,
-%  commentstyle=\itshape\color{Gray},
-%  stringstyle=\color{Black},
-%  numberstyle=\color{Orange},
-%  keywordstyle=\bfseries\color{OliveGreen},
-%  identifierstyle=\color{blue},
-%}        
-
-\definecolor{darkgreen}{rgb}{0,0.6,0}
-\definecolor{darkpink}{rgb}{0.75,0.25,0.5}
-
-\lstdefinestyle{customasm}{
-  language=[ISO]C++,
-  breaklines=true, %% Wrap long lines
-  keywordstyle=\color{blue}\bfseries,
-  commentstyle=\color{darkgreen}\textit,
-  stringstyle=\color{darkpink}\ttfamily,
-  basicstyle=\footnotesize\ttfamily\color{red},
-  identifierstyle={\color{black}},
-%
-  literate=*
-  {;}{{{\color{black};}}}{1}
-  {.7}{{{\color{red}.7}}}{2},%
-%
-morekeywords={int32_t}
-}
-
-\usepackage[colorlinks=true,linkcolor=blue]{hyperref} 
-\begin{document}
-\tableofcontents
-
-EOF
-# xleftmargin=-8em,
-
-past_dir_name="00"
-
-find . -type f \( -iname \*.cpp -o -iname \*.cu \) |
-
-# Change ./foo/bar.src to foo/bar.src
-sed 's/^\..//' |
-
-# Loop through each file
-while read  i; do
-
-  dir_name=`echo $i | awk -F '/' '{print $1}' | sed 's/_/\\\_/g'`
-  file_name=`echo $i | awk -F '/' '{print $2}' | sed 's/_/\\\_/g'`
-
-  if [ "$dir_name" != "$past_dir_name" ]; then
-
-    # Start new section
-    echo "\newpage" >> $tex_file
-    echo "\section{$dir_name}" >> $tex_file
-  else
-    echo "\\" >> $tex_file
-  fi
-
-  # Create a section for each file
-  echo "\subsection{$file_name}" >> $tex_file
-
-  # This command will include the file in the PDF
-  echo "\lstinputlisting[style=customasm]{$i}" >>$tex_file
-
-  past_dir_name="$dir_name"
-
-done &&
-
-echo "\end{document}" >> $tex_file &&
-
-# This needs to be run twice for the TOC to be generated
-pdflatex $tex_file -output-directory . && 
-pdflatex $tex_file -output-directory .
-
-mv tmp.pdf caderno.pdf
-rm tmp.*
diff --git a/algorithms/graph/articulations_bridges.cpp b/algorithms/graph/articulations_bridges.cpp
index 62b0868..7ddb90e 100644
--- a/algorithms/graph/articulations_bridges.cpp
+++ b/algorithms/graph/articulations_bridges.cpp
@@ -1,5 +1,5 @@
 /**
- * Articulations and Bridges (Tarjan)
+ * Tarjan - Articulations and Bridges
  *
  * Complexity (Time): O(n + m)
  * Complexity (Space): O(n + m)
diff --git a/algorithms/graph/bfs.cpp b/algorithms/graph/bfs.cpp
index e57da24..6431f2b 100644
--- a/algorithms/graph/bfs.cpp
+++ b/algorithms/graph/bfs.cpp
@@ -1,5 +1,5 @@
 /**
- * Breadth First Search - BFS
+ * Breadth First Search (BFS)
  *
  * Complexity (Time): O(n + m)
  * Complexity (Space): O(n + m)
diff --git a/algorithms/graph/dfs.cpp b/algorithms/graph/dfs.cpp
index 46373f8..6408d40 100644
--- a/algorithms/graph/dfs.cpp
+++ b/algorithms/graph/dfs.cpp
@@ -1,5 +1,5 @@
 /**
- * Depth First Search - DFS
+ * Depth First Search (DFS)
  *
  * Complexity (Time): O(n + m)
  * Complexity (Space): O(n + m)
diff --git a/algorithms/graph/lca.cpp b/algorithms/graph/lca.cpp
index 5f43a33..8919ff7 100644
--- a/algorithms/graph/lca.cpp
+++ b/algorithms/graph/lca.cpp
@@ -1,9 +1,9 @@
 /**
  * Lowest Common Ancestor - LCA
  *
- * Complexity (Time): 
- *   preprocess -> O(n log n)
- *   query      -> O(log n)
+ * Complexity (Time):
+ *   - preprocess: O(n log n)
+ *   - query:      O(log n)
  * Complexity (Space): O(n + m + n log n)
  *
  * OBS: *   = return sum path to LCA
diff --git a/algorithms/paradigm/kadane.cpp b/algorithms/paradigm/kadane.cpp
index 46d5b4e..dbd4fcd 100644
--- a/algorithms/paradigm/kadane.cpp
+++ b/algorithms/paradigm/kadane.cpp
@@ -1,5 +1,5 @@
 /**
- * Kadane - Largest Sum Contiguous Subarray
+ * Kadane
  *
  * Complexity (Time): O(n + m)
  * Complexity (Space): O(n + m)
diff --git a/algorithms/structure/ball_tree.cpp b/algorithms/structure/ball_tree.cpp
index 7cf6ead..9518ea5 100644
--- a/algorithms/structure/ball_tree.cpp
+++ b/algorithms/structure/ball_tree.cpp
@@ -1,9 +1,9 @@
- /**
-  * Balltree (k-Nearest Neighbors)
-  *
-  * Complexity (Time): O(n log n)
-  * Complexity (Space): O(n)
-  */
+/**
+ * Balltree
+ *
+ * Complexity (Time): O(n log n)
+ * Complexity (Space): O(n)
+ */
 
 #define x first
 #define y second
diff --git a/notebook/gen_latex.py b/notebook/gen_latex.py
new file mode 100644
index 0000000..03ffba5
--- /dev/null
+++ b/notebook/gen_latex.py
@@ -0,0 +1,97 @@
+import os
+import sys
+import subprocess
+import argparse
+
+from pathlib import Path
+
+dirs = [
+    'algorithms',
+    'misc',
+#    'contests',
+#    'problems'
+]
+
+parser = argparse.ArgumentParser()
+parser.add_argument('--header', action='store', type=str, help='The text to parse.')
+parser.add_argument('--output', action='store', type=str, help='The text to parse.')
+args = parser.parse_args()
+
+output = open(args.output, 'w')
+
+# Read Latex header
+with open(args.header) as f:
+    data = f.readlines()
+
+# Print header to output
+for i in data:
+    output.write(i)
+
+last_sections = [None] * 2
+
+for di in dirs: 
+    path_list = Path(di).glob('**/*.cpp')
+
+    for path in path_list:
+        file_name = str(path)
+        sections = file_name.replace('_', '\_').split('/')
+
+        # Sections[0] is [algorithms, contests, problems, misc] (sections)
+        if sections[0] != last_sections[0]:
+            output.write('\\newpage\n')
+            output.write('\\section{' + sections[0].capitalize() + '}\n')
+
+        # Sections[1] is name of constest or category of algorithm (subsections)
+        if sections[1] != last_sections[1]:
+            output.write('\\subsection{' + sections[1].capitalize() + '}\n')
+
+        # Parse source code
+        with open(file_name) as f:
+            source = f.readlines()
+
+        # Separate into comment and code, and define title
+        title = ""
+        in_comment = False
+        code, comment = [], []
+        for line in source:
+            if '/**' == line[0:3]:
+                in_comment = True
+
+            if in_comment:
+                if len(title) == 0 and len(comment) == 1:
+                    title = line[3:]
+                comment.append(line)
+            else:
+                code.append(line)
+
+            if '*/' in line:
+                in_comment = False
+
+        in_ctime = False
+        ctime, cspace = [], []
+        for line in comment:
+            if 'Complexity (time)' in line:
+                in_ctime = True
+            if 'Complexity (space)' in line:
+                in_ctime = False
+
+            if in_ctime:
+                ctime.append(line)
+
+
+
+        if len(sections) > 2:
+            output.write('\\subsubsection{' + title + '}\n')
+
+        # Remove first \n after header comment
+        code = code[1:]
+
+        #output.write('\\lstinputlisting[style=customcpp]{' + file_name + '}\n')
+        output.write('\\begin{lstlisting}[style=customcpp]\n')
+        for i in code:
+            output.write(i)
+        output.write('\\end{lstlisting}\n')
+        output.write('\\\n')
+        last_sections = sections
+
+output.write('\\end{document}')
diff --git a/notebook/header.tex b/notebook/header.tex
new file mode 100644
index 0000000..655b998
--- /dev/null
+++ b/notebook/header.tex
@@ -0,0 +1,22 @@
+\documentclass{article}
+\usepackage{listings}
+\usepackage{titlesec}
+\usepackage[a4paper, total={6in, 8in}]{geometry}
+\geometry{
+    a4paper,
+    left=20mm,
+    right=10mm,
+    top=1in,
+    bottom=1in,
+}
+\titleformat*{\subsubsection}{\Large\bfseries}
+\lstdefinestyle{customcpp}{
+    frame=single,
+    language=C++,
+    basicstyle=\small,
+    tabsize=2,
+    breaklines=true,
+    morekeywords={int32\_t, ll}
+}
+\begin{document}
+\tableofcontents
diff --git a/run b/run
new file mode 100755
index 0000000..99c4c76
--- /dev/null
+++ b/run
@@ -0,0 +1,10 @@
+#!/bin/bash
+
+tex_file=$(mktemp)
+python3 notebook/gen_latex.py --header=notebook/header.tex --output=$tex_file
+
+pdflatex $tex_file -output-directory . && 
+pdflatex $tex_file -output-directory .
+
+mv tmp.pdf caderno.pdf
+rm tmp*
-- 
GitLab