Giter VIP home page Giter VIP logo

anantkm / cpp2pdf Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 4 KB

This Python script automates the conversion of student C++ file submissions to PDF format. It extracts student email IDs from file names, maps files to specific output directories, highlights code syntax, and generates PDF files. It's designed to streamline the process of uploading student work to platforms like Gradescope.

Python 100.00%

cpp2pdf's Introduction

Code Documentation: Introduction

This script serves the specific purpose of automating the conversion of C++ source files into PDF format for University of California, Riverside (UCR) courses. It offers syntax highlighting and integrates the student's NetID extracted from the filename, enhancing its utility for instructors and TAs in evaluating code submissions. It operates under the assumption that student emails adhere to the pattern [email protected], common in UCR courses.

Additionally, instructors utilizing PrairieLearn's pl-file-editor element may find this script beneficial for converting cpp files to pfs, facilitating the upload of files to Gradescope for grading purposes.

Features

  • A log file that lists any files that were skipped.
  • Compatibility with both CPP and .h file formats.
  • A dedicated folder for organizing header files.

Prerequisites

Before you can use this script, ensure you have the following installed:

  • Python 3.9.XX
  • Pygments
  • WeasyPrint

Note: WeasyPrint has specific installation requirements that might not be resolved with pip alone. Please refer to the official WeasyPrint documentation for OS specific installation instructions.

Usage

  1. Set the source_directory variable to the path containing your .cpp files.
  2. Set the base_output_directory variable to the path where you want the PDFs to be saved.
    • The script will create the output directory if it doesn't exist.
  3. Update target_folders_name based on the keywords in file name
  4. Run the script:
    python cpp2pdf_converter.py
    

Function Descriptions

  1. extract_email(filename): Extracts and returns the student's NetID from the given filename, assuming the email follows the pattern [email protected].

  2. determine_target_dir(filename, base_output_dir, target_folders_name): Determines the target directory for the output PDF based on the filename and predefined folder names. Appends '/header' to the folder path for header files.

  3. read_file(file_path): Reads and returns the content of a file located at file_path.

  4. convert_code_to_html(code, student_email): Converts the given code string to syntax-highlighted HTML, including the student's NetID.

  5. write_pdf(html_content, output_file_path): Converts the given HTML content to a PDF file at the specified output path.

  6. convert_to_pdf(source_dir, base_output_dir, target_folders_name, file_extensions): Main function that iterates through files in the source directory, converts them to PDF if they match the specified extensions, and organizes them into target directories.

Usage Example

To use this script, define the source directory containing the C++ files, the base output directory for the PDFs, the mapping of keywords to target folder names, and the file extensions to be converted. Then, call convert_to_pdf with these parameters.

file_extensions = {".cpp": ".pdf", ".h": "_h.pdf"}

source_directory = "/path/to/source"
base_output_directory = "/path/to/output"

target_folders_name = {
    "Keyword1": "Folder1",
    "Keyword2": "Folder2",
    # Add more mappings as needed
}

convert_to_pdf(source_directory, base_output_directory, target_folders_name, file_extensions)

Additional Notes

  • Ensure that the source directory and base output directory are correctly specified.
  • The script assumes that the student's email is embedded in the filename and follows the [email protected] pattern.
  • Skipped files (those without a matching target directory or unsupported extensions) are logged in files_skipped.txt in the base output directory.

Compatibility and Tested Versions

This program has been tested and confirmed to work with the following software versions:

  • Python: 3.9.12
  • Pygments: 2.11.2
  • WeasyPrint: 60.2
  • MacOS: 14.2.1

cpp2pdf's People

Contributors

anantkm avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.