bashshellpdfsyntax-highlighting

Conversion of Source Code to PDF from GitHub


I am trying to take all files from a specific repository I have on GitHub and turn them into PDF's. I have read and seen an example here

However, I am still a bit confused on how to go about doing this. I have to take all my files and turn them into a single PDF file to upload it to a site for college.

I am not extremely familiar with UNIX commands and I was trying to do the following:

for i in *.lua; do echo "$i"; echo "---"; cat "$i"; echo ; done > result.pdf

Then I was going to take all the pdfs and merge them together, but I was hoping there was a better way to go about doing this. I'm specifically dealing with only .lua and .md file-extensions.

I personally believe this can be done with the use of some UNIX commands, but as mentioned, I am not familiar with them.

To summarize, my main objective is to take a series of files located on a repository on Github and merge them into one PDF file. Even obtaining multiple PDF files is ok is that is the best that can be done. Even a .word file format is good enough.

Operating System: OSX or Windows 7 64-bit


Solution

  • I suggest looking into a syntax highlighter. Pygments has the ability to generate HTML, RTF, etc. If you chose to output HTML files, you could then use a tool like wkhtmltopdf to convert the syntax-highlighted HTML files to PDF files.

    Alternatively, Pygments can do LaTeX. If you're familiar with LaTeX, then you can have Pygments generate LaTeX output, and use pdflatex to generate your PDF files.

    You said you're on OS X. To install Pygments, open a terminal and type:

    sudo easy_install Pygments
    

    This will install a program pygmentize that you can use to transform code.

    Next, install wkhtmltopdf.

    Now, you can take a file, syntax highlight it, and convert it to PDF:

    pygmentize -l ruby -f html -O full,style=vim test.rb > test.html
    wkhtmltopdf test.html test.pdf
    

    Here, I show the conversion of a Ruby script. Of course, you'd want to use -l lua if you're converting Lua scripts.

    You can then incorporate these commands into a shell script that traverses over a directory recursively, e.g.

    #!/bin/bash
    
    # Change this to the repository directory
    REPOSITORY=/path/to/the/repo
    
    # Iterate over the repository
    while read source_file
    do
      filename=$(basename $source_file)
      dir=$(dirname $source_file)
    
      # For each .lua file found, generate an HTML file in /tmp
      pygmentize -l lua -f html -O full,style=vim $source_file > /tmp/${filename}.html
    
      # Convert the HTML file to a PDF file in the same directory 
      # as the .lua file
      wkhtmltopdf /tmp/${filename}.html ${dir}/${filename}.pdf
    
    done < <(find $REPOSITORY -type f -iname '*.lua')
    

    Put this in a file named convert.sh. Then, to run it, type:

    chmod +x convert.sh
    ./convert.sh