5

I'm trying to write a shellscript which prints a tree-view for a specific directory and it's subdirs to pdf via latex, as well as the titles and contents of all scripts contained within that primary directory.

The treeview works like a charm, but I have no idea how to get the printing of the scripts to work.

Code thus far:

#!/bin/bash

# Script to export directory with pdflatex

# Generate .tex file
# Directory Listing
echo "\documentclass[11pt,a4paper,oneside]{article}" > tmp.tex
echo "\usepackage{fullpage}" >> tmp.tex
echo "\begin{document}" >> tmp.tex
echo "\section{Listing}" >> tmp.tex
echo "\begin{verbatim}" >> tmp.tex
tree $1 >> tmp.tex
echo "\end{verbatim}" >> tmp.tex
echo "\end{document}" >> tmp.tex

# ShellScript printout
???????

# Generate .pdf file
pdflatex tmp.tex

#Cleanup
rm tmp.tex
viraxis
  • 51
  • 1
  • 3

2 Answers2

3

Pretty print directory tree and script files contents

Edit: Newer version with full table of content as tree and picture support, in second part.

Using \verbatiminput from package verbatim.

Like this:

#!/bin/bash

tempfile=$(mktemp /tmp/dirtree-XXXXX.tex)
trap "rm $tempfile" 0 1 2 3 6 9 15

cat <<EOF >$tempfile
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage,verbatim,dirtree}
\begin{document}
\section{Listing}
\dirtree{%
EOF

export -a scriptList=()
while IFS=/ read -a fPath ;do
    file="${fPath[*]:${#fPath[*]}-1}"
    IFS=/
    full="${fPath[*]}"
    type="$(file -b "$full")"
    echo .${#fPath[@]} "${file//_/\\_}\DTcomment{$type}. "
    [[ "$type" =~ script.text ]] && scriptList=("${scriptList[@]}" "$full")
    done  < <(
    find $1 -type d -o -type f
)  >>$tempfile

export IFS=$'\n\t '
echo "}" >>$tempfile

for file in "${scriptList[@]}";do
    name="${file##*/}"
    printf "\\section{%s}\n{\\\\scriptsize\\\\verbatiminput{%s}}\n" \
    "${name//_/\_}" "${file}"  >>"${tempfile}"    
done

echo >>"${tempfile}" '\end{document}'

pdflatex -interaction nonstopmode "${tempfile}"

Who would produce this kind of output:

print directory content

Pretty print directory tree with table of content, scripts and images files.

NOTA: For computing toc, latex have to be run two times.

bugs:

This script is only a proof of concept, type of images is probably limited and could be improved, eventualy by the help of imagemagik, netpbm or any graphic lib... and so on

todo:

  • fix dimensions of images
  • improve images filtering
  • add support for pdf, ps and maybe other printable like .man, .tex, .sgml, .odf
    • add option for printing first page of document files.
  • make and purge temporary files more properly.

There it is:

#!/bin/bash

tempfile=$(mktemp /tmp/dirtree-XXXXX.tex)
# trap "rm $tempfile" 0 1 2 3 6 9 15

cat <<EOF >$tempfile
\documentclass[11pt,a4paper,oneside]{article}
\usepackage{fullpage,graphicx,verbatim,dirtree}
\makeatletter
\newcommand{\typePPage}[2]{\DTcomment{{\scriptsize #1
\begin{minipage}[t]{5em}\mbox{}\hfill\ifx\@empty#2\else%
s.$\ref{sec:#2}$, p.$\pageref{sec:#2}$\fi\end{minipage}}}}
\makeatother
\begin{document}\parindent=0pt%
\section{Listing}
\dirtree{%
EOF

export -a scriptList=()
export -A typelist=()
while IFS=/ read -a fPath ;do
    file="${fPath[*]:${#fPath[*]}-1}"
    IFS=/
    full="${fPath[*]}"
    type="$(file -b "$full")"
    if [[ "$type" =~ script.text ]] || [[ "$type" =~ image ]] ;then
    scriptList=("${scriptList[@]}" "$full")
    typelist["${full//\//_}"]="$type"
    echo .${#fPath[@]} \
        "${file//_/\\_}\typePPage{$type}{${file//[\/.+()_-]/}}. "
    else
    echo .${#fPath[@]} "${file//_/\\_}\typePPage{$type}{}. "
    fi
    done  < <(
    find $1 -type d -o -type f
)  >>$tempfile

export IFS=$'\n\t '
echo "}" >>$tempfile

for file in "${scriptList[@]}";do
    name="${file##*/}"
    printf '\\section{%s}\n\\label{sec:%s}\n' \
    "${name//_/\_}" "${name//[\/.+()_-]/}"
    if [[ "${typelist["${file//\//_}"]}" =~ script.text ]];then
    printf '{\\scriptsize\\verbatiminput{%s}}\n' "${file}"
    else
    printf '\\includegraphics[width=\\textwidth]{%s}\n' "${file}"
    fi
done >>"${tempfile}"

echo >>"${tempfile}" '\end{document}'

pdflatex -interaction nonstopmode "${tempfile}" >/dev/null 2>&1
pdflatex -interaction nonstopmode "${tempfile}"

Could produce:

Sample of output

0

It is not exactly clear to me what you want, but as I understand it, you want to have the output of all .sh files printed into tmp.tex. Then you could do something like

find -name "*.sh" -exec cat "{}" + >> tmp.tex

Which concatenate all .sh files, and then adds it to tmp.tex.

Bernhard
  • 11,992
  • 4
  • 59
  • 69