├── .coafile ├── .gitignore ├── .misc ├── install.sh ├── tests.sh └── travis.sh ├── .rultor.yml ├── .travis.yml ├── Code2pdf ├── __init__.py └── code2pdf.py ├── Demo └── demo.pdf ├── Jenkinsfile ├── LICENSE ├── MANIFEST.in ├── README ├── README.md ├── README.rst ├── requirements.txt ├── setup.cfg ├── setup.py └── tests ├── __init__.py └── test.py /.coafile: -------------------------------------------------------------------------------- 1 | [Default] 2 | bears = SpaceConsistencyBear 3 | files = *.py 4 | use_spaces = true 5 | 6 | [Code2pdf] 7 | bears = LineLengthBear 8 | max_line_length = 79 9 | 10 | [python] 11 | bears = PEP8Bear 12 | files = *.py 13 | default_actions = PEP8Bear:ApplyPatchAction 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.orig 2 | *~ 3 | *.pyc 4 | .cache/* 5 | .coverage 6 | build/* 7 | dist/* 8 | *.egg-info -------------------------------------------------------------------------------- /.misc/install.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | sudo apt-get install openssl build-essential xorg libssl-dev python-qt4 -------------------------------------------------------------------------------- /.misc/tests.sh: -------------------------------------------------------------------------------- 1 | set -e 2 | set -x 3 | 4 | python tests/test.py 5 | -------------------------------------------------------------------------------- /.misc/travis.sh: -------------------------------------------------------------------------------- 1 | check_install_xvfb() { 2 | if hash xvfb-run 2>/dev/null; then 3 | : 4 | else 5 | sudo apt-get update 6 | sudo apt-get upgrade 7 | sudo apt-get install xvfb 8 | fi 9 | } 10 | check_install_xvfb 11 | export DISPLAY=localhost:1.0 12 | xvfb-run -a bash .misc/tests.sh 13 | -------------------------------------------------------------------------------- /.rultor.yml: -------------------------------------------------------------------------------- 1 | merge: 2 | fast-forward: only -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: required 2 | language: python 3 | python: 4 | - "2.7_with_system_site_packages" # For PyQt4 5 | before_install: 6 | - pip install setuptools --upgrade 7 | - pip install -r requirements.txt 8 | install: 9 | - bash .misc/install.sh 10 | - python setup.py install 11 | script: 12 | - bash .misc/travis.sh 13 | notifications: 14 | email: false 15 | -------------------------------------------------------------------------------- /Code2pdf/__init__.py: -------------------------------------------------------------------------------- 1 | from .code2pdf import main, Code2pdf, get_output_file 2 | -------------------------------------------------------------------------------- /Code2pdf/code2pdf.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | from PyQt5.QtWidgets import QApplication 3 | from PyQt5.QtPrintSupport import QPrinter 4 | from PyQt5.QtGui import QTextDocument 5 | import argparse 6 | import logging 7 | import os 8 | import re 9 | import sys 10 | 11 | try: 12 | import pygments 13 | from pygments import lexers, formatters, styles 14 | except ImportError as ex: 15 | logging.warning('\nCould not import the required "pygments" \ 16 | module:\n{}'.format(ex)) 17 | sys.exit(1) 18 | 19 | __version__ = '1.1.0' 20 | 21 | 22 | def logger(func): 23 | def log_wrap(self, ifile=None, ofile=None, size="A4"): 24 | logging.getLogger().name = "code2pdf> " 25 | logging.getLogger().setLevel(logging.INFO) 26 | func(self, ifile, ofile, size) 27 | return log_wrap 28 | 29 | 30 | class Code2pdf: 31 | 32 | """ 33 | Convert a source file into a pdf with syntax highlighting. 34 | """ 35 | @logger 36 | def __init__(self, ifile=None, ofile=None, size="A4"): 37 | self.size = size 38 | if not ifile: 39 | raise Exception("input file is required") 40 | self.input_file = ifile 41 | self.pdf_file = ofile or "{}.pdf".format(ifile.split('.')[0]) 42 | 43 | def highlight_file(self, linenos=True, style='default'): 44 | """ Highlight the input file, and return HTML as a string. """ 45 | try: 46 | lexer = lexers.get_lexer_for_filename(self.input_file) 47 | except pygments.util.ClassNotFound: 48 | # Try guessing the lexer (file type) later. 49 | lexer = None 50 | 51 | try: 52 | formatter = formatters.HtmlFormatter( 53 | linenos=linenos, 54 | style=style, 55 | full=True) 56 | except pygments.util.ClassNotFound: 57 | logging.error("\nInvalid style name: {}\nExpecting one of:\n \ 58 | {}".format(style, "\n ".join(sorted(styles.STYLE_MAP)))) 59 | sys.exit(1) 60 | 61 | try: 62 | with open(self.input_file, "r") as f: 63 | content = f.read() 64 | try: 65 | lexer = lexer or lexers.guess_lexer(content) 66 | except pygments.util.ClassNotFound: 67 | # No lexer could be guessed. 68 | lexer = lexers.get_lexer_by_name("text") 69 | except EnvironmentError as exread: 70 | fmt = "\nUnable to read file: {}\n{}" 71 | logging.error(fmt.format(self.input_file, exread)) 72 | sys.exit(2) 73 | 74 | return pygments.highlight(content, lexer, formatter) 75 | 76 | def init_print(self, linenos=True, style="default"): 77 | app = QApplication([]) # noqa 78 | doc = QTextDocument() 79 | doc_html = self.highlight_file(linenos=linenos, style=style) 80 | doc_html = re.sub(re.compile(r''), '', doc_html) 81 | doc.setHtml(doc_html) 82 | printer = QPrinter() 83 | printer.setOutputFileName(self.pdf_file) 84 | printer.setOutputFormat(QPrinter.PdfFormat) 85 | page_size_dict = {"a2": QPrinter.A2, "a3": QPrinter.A3, "a4": QPrinter.A4, "letter": QPrinter.Letter} 86 | printer.setPageSize(page_size_dict.get(self.size.lower(), QPrinter.A4)) 87 | printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter) 88 | doc.print_(printer) 89 | logging.info("PDF created at %s" % (self.pdf_file)) 90 | 91 | 92 | def get_output_file(inputname, outputname=None): 93 | """ If the output name is set, then return it. 94 | Otherwise, build an output name using the current directory, 95 | replacing the input name's extension. 96 | """ 97 | if outputname: 98 | return outputname 99 | 100 | inputbase = os.path.split(inputname)[-1] 101 | outputbase = "{}.pdf".format(os.path.splitext(inputbase)[0]) 102 | return os.path.join(os.getcwd(), outputbase) 103 | 104 | 105 | def parse_arg(): 106 | parser = argparse.ArgumentParser( 107 | description=( 108 | "Convert given source code into .pdf with syntax highlighting"), 109 | epilog="Author:tushar.rishav@gmail.com" 110 | ) 111 | parser.add_argument( 112 | "filename", 113 | help="absolute path of the python file", 114 | type=str) 115 | parser.add_argument( 116 | "-l", 117 | "--linenos", 118 | help="include line numbers.", 119 | action="store_true") 120 | parser.add_argument( 121 | "outputfile", 122 | help="absolute path of the output pdf file", 123 | nargs="?", 124 | type=str) 125 | parser.add_argument( 126 | "-s", 127 | "--size", 128 | help="PDF size. A2,A3,A4,A5,letter etc", 129 | type=str, 130 | default="A3") 131 | parser.add_argument( 132 | "-S", 133 | "--style", 134 | help="the style name for highlighting.", 135 | type=str, 136 | default="default", 137 | metavar="NAME") 138 | parser.add_argument( 139 | "-v", 140 | "--version", 141 | action="version", 142 | version="%(prog)s v. {}".format(__version__)) 143 | return parser.parse_args() 144 | 145 | 146 | def main(): 147 | args = parse_arg() 148 | pdf_file = get_output_file(args.filename, args.outputfile) 149 | pdf = Code2pdf(args.filename, pdf_file, args.size) 150 | pdf.init_print(linenos=args.linenos, style=args.style) 151 | return 0 152 | 153 | if __name__ == "__main__": 154 | sys.exit(main()) 155 | -------------------------------------------------------------------------------- /Demo/demo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tushar-rishav/code2pdf/041b192f7c201ac327febace7d84d1489711c1bc/Demo/demo.pdf -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent any 3 | stages { 4 | stage('Build') { 5 | steps { 6 | echo 'Building..' 7 | } 8 | } 9 | stage('Test') { 10 | steps { 11 | echo 'Testing..' 12 | } 13 | } 14 | stage('Deploy') { 15 | steps { 16 | echo 'Deploying....' 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [2015] [Tushar Gautam] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include *.txt 2 | recursive-include *.py 3 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | Code2pdf 2 | -------------- 3 | 4 | Convert various source code into pdf file with syntax highlighting and many more 5 | features 6 | 7 | `Demo`_ 8 | ------- 9 | Click above for demo 10 | 11 | Dependencies 12 | ~~~~~~~~~~~~ 13 | 14 | `PyQt `__ 15 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 16 | 17 | Installation 18 | ~~~~~~~~~~~~ 19 | 20 | Build from source 21 | ''''''''''''''''' 22 | 23 | .. code:: sh 24 | 25 | git clone https://github.com/tushar-rishav/code2pdf.git 26 | cd code2pdf 27 | python setup.py install 28 | 29 | Or 30 | 31 | Using pip 32 | ''''''''' 33 | 34 | .. code:: sh 35 | 36 | pip install code2pdf 37 | 38 | Usage 39 | ~~~~~ 40 | 41 | A. As console app 42 | ''''''''''''''''' 43 | 44 | Help 45 | 46 | 47 | .. code:: sh 48 | 49 | code2pdf -h 50 | 51 | Usage 52 | 53 | 54 | ``code2pdf [-h] [-l] [-s SIZE] [-S NAME] [-v] filename [outputfile]`` 55 | 56 | Options 57 | 58 | 59 | .. code:: sh 60 | 61 | positional arguments: 62 | filename absolute path of the python file 63 | outputfile absolute path of the output pdf file 64 | 65 | optional arguments: 66 | -h, --help show this help message and exit 67 | -l, --linenos include line numbers. 68 | -s SIZE, --size SIZE PDF size. A2,A3,A4,A5 etc 69 | -S NAME, --style NAME 70 | the style name for highlighting. Eg. emacs, vim style etc. 71 | -v, --version show program's version number and exit 72 | 73 | Available style types are 74 | 75 | 76 | - [x] autumn 77 | - [x] borland 78 | - [x] bw 79 | - [x] colorful 80 | - [x] default 81 | - [x] emacs 82 | - [x] friendly 83 | - [x] fruity 84 | - [x] igor 85 | - [x] manni 86 | - [x] monokai 87 | - [x] murphy 88 | - [x] native 89 | - [x] paraiso-dark 90 | - [x] paraiso-light 91 | - [x] pastie 92 | - [x] perldoc 93 | - [x] rrt 94 | - [x] tango 95 | - [x] trac 96 | - [x] vim 97 | - [x] vs 98 | - [x] xcode 99 | 100 | Example 101 | 102 | 103 | .. code:: sh 104 | 105 | code2pdf -l -s a3 -S emacs ~/Code2Pdf/Code2pdf/code2pdf.py ~/Code2Pdf/Demo/demo.pdf 106 | 107 | To see the demo for above check ``Demo/`` in github repo 108 | 109 | B. As module 110 | '''''''''''' 111 | 112 | .. code:: py 113 | 114 | 115 | from Code2pdf.code2pdf import Code2pdf 116 | ifile,ofile,size = "test.py", "test.pdf", "A4" 117 | pdf = Code2pdf(ifile, ofile, size) # create the Code2pdf object 118 | pdf.init_print() # call print method to print pdf 119 | 120 | Contributions 121 | ~~~~~~~~~~~~~ 122 | 123 | Have an idea to make it better? Go ahead! I will be happy to see a pull 124 | request from you! :blush: 125 | While creating a PR, please update the *Contributor* section too( see below ). 126 | 127 | Contributor 128 | ~~~~~~~~~~~ 129 | 130 | `Christopher Welborn `__ 131 | 132 | `cclauss `__ 133 | 134 | .. _Demo: https://cloud.githubusercontent.com/assets/7397433/10060934/645a3cc6-6272-11e5-9ebb-a1ac24c86d67.gif 135 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Code2pdf :fax: 2 | Convert given source code into .pdf with syntax highlighting and more features 3 | 4 | | Build Status | Version | Downloads | Python | 5 | | ------------ |---------|-----------|----------| 6 | | [![Build Status](https://travis-ci.org/tushar-rishav/code2pdf.svg?branch=master)](https://travis-ci.org/tushar-rishav/code2pdf)|[![PyPI version](https://badge.fury.io/py/Code2pdf.svg)](http://badge.fury.io/py/Code2pdf)| [![PyPi downloads](https://img.shields.io/pypi/dw/code2pdf.svg)](https://pypi.python.org/pypi/Code2pdf)|[![PyPI](https://img.shields.io/pypi/pyversions/Code2pdf.svg)](https://pypi.python.org/pypi/Py2pdf) 7 | 8 | ### [Demo](https://cloud.githubusercontent.com/assets/7397433/10060934/645a3cc6-6272-11e5-9ebb-a1ac24c86d67.gif) 9 | ![demo](https://cloud.githubusercontent.com/assets/7397433/10060934/645a3cc6-6272-11e5-9ebb-a1ac24c86d67.gif) 10 | 11 | ### Installation 12 | 13 | ##### Build from source 14 | 15 | ```sh 16 | git clone https://github.com/tushar-rishav/code2pdf.git 17 | cd code2pdf 18 | pip3 install . 19 | 20 | ``` 21 | Or 22 | 23 | ##### Using pip 24 | 25 | ```sh 26 | pip3 install code2pdf 27 | 28 | ``` 29 | ### Usage 30 | 31 | ##### A. As console app 32 | 33 | ###### Help 34 | 35 | ```sh 36 | code2pdf -h 37 | 38 | ``` 39 | ###### Usage 40 | ` code2pdf [-h] [-l] [-s SIZE] [-S NAME] [-v] filename [outputfile] ` 41 | 42 | ###### Options 43 | 44 | ```sh 45 | positional arguments: 46 | filename absolute path of the python file 47 | outputfile absolute path of the output pdf file 48 | 49 | optional arguments: 50 | -h, --help show this help message and exit 51 | -l, --linenos include line numbers. 52 | -s SIZE, --size SIZE PDF size. A2,A3,A4,A5 etc 53 | -S NAME, --style NAME 54 | the style name for highlighting. Eg. emacs, vim style etc. 55 | -v, --version show program's version number and exit 56 | 57 | ``` 58 | ###### Available style types are 59 | 60 | - [x] autumn 61 | - [x] borland 62 | - [x] bw 63 | - [x] colorful 64 | - [x] default 65 | - [x] emacs 66 | - [x] friendly 67 | - [x] fruity 68 | - [x] igor 69 | - [x] manni 70 | - [x] monokai 71 | - [x] murphy 72 | - [x] native 73 | - [x] paraiso-dark 74 | - [x] paraiso-light 75 | - [x] pastie 76 | - [x] perldoc 77 | - [x] rrt 78 | - [x] tango 79 | - [x] trac 80 | - [x] vim 81 | - [x] vs 82 | - [x] xcode 83 | 84 | ###### Example 85 | ```sh 86 | code2pdf -l -s a3 -S emacs ~/Code2Pdf/Code2pdf/code2pdf.py ~/Code2Pdf/Demo/demo.pdf 87 | 88 | ``` 89 | To see the demo for above check `Demo/` in github repo 90 | 91 | ##### B. As module 92 | 93 | ```py 94 | 95 | from Code2pdf import Code2pdf 96 | ifile,ofile,size = "test.py", "test.pdf", "A4" 97 | pdf = Code2pdf(ifile, ofile, size) # create the Code2pdf object 98 | pdf.init_print() # call print method to print pdf 99 | 100 | ``` 101 | 102 | ### Contributions 103 | 104 | Have an idea to make it better? Go ahead! I will be happy to see a pull request from you! :blush: 105 | >While creating a PR, please update the *Contributor* section too( see below ). 106 | 107 | ### Contributor 108 | [Christopher Welborn](https://github.com/cjwelborn) 109 | 110 | [cclauss](https://github.com/cclauss) 111 | 112 | [Thommy257](https://github.com/Thommy257) 113 | 114 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Code2pdf :fax: 2 | ------------ 3 | 4 | Convert given source code into .pdf with syntax highlighting and more features 5 | 6 | +------------------+------------------+--------------------+----------+ 7 | | Build Status | Version | Downloads | Python | 8 | +==================+==================+====================+==========+ 9 | | |Build Status| | |PyPI version| | |PyPi downloads| | |PyPI| | 10 | +------------------+------------------+--------------------+----------+ 11 | 12 | `Demo `__ 13 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 14 | 15 | .. figure:: https://cloud.githubusercontent.com/assets/7397433/10060934/645a3cc6-6272-11e5-9ebb-a1ac24c86d67.gif 16 | :alt: demo 17 | 18 | Dependencies 19 | ~~~~~~~~~~~~ 20 | 21 | - [x] [PyQt](http://www.riverbankcomputing.com/software/pyqt/download) 22 | 23 | Installation 24 | ~~~~~~~~~~~~ 25 | 26 | Build from source 27 | ''''''''''''''''' 28 | 29 | .. code:: sh 30 | 31 | git clone https://github.com/tushar-rishav/code2pdf.git 32 | cd code2pdf 33 | python setup.py install 34 | 35 | Or 36 | 37 | Using pip 38 | ''''''''' 39 | 40 | .. code:: sh 41 | 42 | pip install code2pdf 43 | 44 | Usage 45 | ~~~~~ 46 | 47 | A. As console app 48 | ''''''''''''''''' 49 | 50 | Help 51 | 52 | 53 | .. code:: sh 54 | 55 | code2pdf -h 56 | 57 | Usage 58 | 59 | 60 | ``code2pdf [-h] [-l] [-s SIZE] [-S NAME] [-v] filename [outputfile]`` 61 | 62 | Options 63 | 64 | 65 | .. code:: sh 66 | 67 | positional arguments: 68 | filename absolute path of the python file 69 | outputfile absolute path of the output pdf file 70 | 71 | optional arguments: 72 | -h, --help show this help message and exit 73 | -l, --linenos include line numbers. 74 | -s SIZE, --size SIZE PDF size. A2,A3,A4,A5 etc 75 | -S NAME, --style NAME 76 | the style name for highlighting. Eg. emacs, vim style etc. 77 | -v, --version show program's version number and exit 78 | 79 | Available style types are 80 | 81 | 82 | - [x] autumn 83 | - [x] borland 84 | - [x] bw 85 | - [x] colorful 86 | - [x] default 87 | - [x] emacs 88 | - [x] friendly 89 | - [x] fruity 90 | - [x] igor 91 | - [x] manni 92 | - [x] monokai 93 | - [x] murphy 94 | - [x] native 95 | - [x] paraiso-dark 96 | - [x] paraiso-light 97 | - [x] pastie 98 | - [x] perldoc 99 | - [x] rrt 100 | - [x] tango 101 | - [x] trac 102 | - [x] vim 103 | - [x] vs 104 | - [x] xcode 105 | 106 | Example 107 | 108 | 109 | .. code:: sh 110 | 111 | code2pdf -l -s a3 -S emacs ~/Code2Pdf/Code2pdf/code2pdf.py ~/Code2Pdf/Demo/demo.pdf 112 | 113 | To see the demo for above check ``Demo/`` in github repo 114 | 115 | B. As module 116 | '''''''''''' 117 | 118 | .. code:: py 119 | 120 | 121 | from Code2pdf.code2pdf import Code2pdf 122 | ifile,ofile,size = "test.py", "test.pdf", "A4" 123 | pdf = Code2pdf(ifile, ofile, size) # create the Code2pdf object 124 | pdf.init_print() # call print method to print pdf 125 | 126 | Contributions 127 | ~~~~~~~~~~~~~ 128 | 129 | Have an idea to make it better? Go ahead! I will be happy to see a pull 130 | request from you! :blush: 131 | While creating a PR, please update the *Contributor* section too( see below ). 132 | 133 | Contributor 134 | ~~~~~~~~~~~ 135 | 136 | `Christopher Welborn `__ 137 | 138 | `cclauss `__ 139 | 140 | License 141 | ~~~~~~~ 142 | 143 | .. figure:: https://cloud.githubusercontent.com/assets/7397433/9025904/67008062-3936-11e5-8803-e5b164a0dfc0.png 144 | :alt: gpl 145 | 146 | 147 | .. |Build Status| image:: https://travis-ci.org/tushar-rishav/code2pdf.svg?branch=master 148 | :target: https://travis-ci.org/tushar-rishav/code2pdf 149 | .. |PyPI version| image:: https://badge.fury.io/py/Code2pdf.svg 150 | :target: http://badge.fury.io/py/Code2pdf 151 | .. |PyPi downloads| image:: https://img.shields.io/pypi/dw/code2pdf.svg 152 | :target: https://pypi.python.org/pypi/Py2pdf 153 | .. |PyPI| image:: https://img.shields.io/pypi/pyversions/Code2pdf.svg 154 | :target: https://pypi.python.org/pypi/Py2pdf 155 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Pygments 2 | unittest2 3 | pyqt5 -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | try: 2 | from setuptools import setup 3 | except ImportError: 4 | from distutils.core import setup 5 | 6 | import sys 7 | import os 8 | 9 | extra = {} 10 | if sys.version_info >= (3,): 11 | extra['use_2to3'] = True 12 | setup(name='Code2pdf', 13 | version='1.1.0', 14 | install_requires=[ 15 | r for r in open('requirements.txt', 'r').read().split('\n') if r], 16 | author='Tushar Gautam', 17 | author_email='tushar.rishav@gmail.com', 18 | packages=['Code2pdf', ], 19 | entry_points={ 20 | 'console_scripts': ['code2pdf=Code2pdf:main'], 21 | }, 22 | license='GNU General Public License v3 (GPLv3)', 23 | url='https://github.com/tushar-rishav/code2pdf/', 24 | description="Converts given source code into pdf file with syntax \ 25 | highlighting, line numbers and much more", 26 | long_description=open('README').read(), 27 | keywords=['pdf', 'markup', 28 | 'formatting', 'convert code to pdf', 'python'], 29 | classifiers=[ 30 | 'Operating System :: POSIX :: Linux', 31 | 'Programming Language :: Python', 32 | 'Programming Language :: Python :: 3', 33 | 'Topic :: System :: Monitoring' 34 | ], 35 | ) 36 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tushar-rishav/code2pdf/041b192f7c201ac327febace7d84d1489711c1bc/tests/__init__.py -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | import os 2 | import unittest2 3 | import sys 4 | import logging 5 | from Code2pdf.code2pdf import Code2pdf, get_output_file 6 | try: 7 | import pygments 8 | from pygments import lexers, formatters, styles 9 | except ImportError as ex: 10 | logging.warning('\nCould not import the required \ 11 | "pygments" module:\n{}'.format( 12 | ex)) 13 | sys.exit(1) 14 | 15 | 16 | class Code2pdfTestCase(unittest2.TestCase): 17 | 18 | """ 19 | Tests for `code2pdf.py`. 20 | """ 21 | 22 | def setUp(self): 23 | self.filename = os.path.abspath(os.path.join( "tests","test.py")) 24 | self.pdf_file = os.path.abspath("test.pdf") 25 | self.size = "a4" 26 | self.style = "emacs" 27 | self.linenos = True 28 | self.output_file = None 29 | 30 | def test_init_print(self): 31 | pdf = Code2pdf(self.filename, self.pdf_file, self.size) 32 | pdf.init_print(self.linenos, self.style) 33 | self.assertTrue(os.path.exists(pdf.pdf_file)) # if pdf printed 34 | 35 | def test_highlight_file(self): 36 | hpdf = Code2pdf(self.filename, self.pdf_file, self.size) 37 | html = hpdf.highlight_file(True, 'emacs') 38 | self.assertIn("!DOCTYPE", html) # if html created 39 | 40 | def test_get_output_file(self): 41 | self.assertEqual( 42 | get_output_file(self.filename, self.output_file), self.pdf_file) 43 | 44 | if __name__ == "__main__": 45 | unittest2.main() 46 | --------------------------------------------------------------------------------