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.
- 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.
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.
- Set the
source_directory
variable to the path containing your.cpp
files. - 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.
- Update
target_folders_name
based on the keywords in file name - Run the script:
python cpp2pdf_converter.py
-
extract_email(filename): Extracts and returns the student's NetID from the given filename, assuming the email follows the pattern [email protected].
-
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.
-
read_file(file_path): Reads and returns the content of a file located at file_path.
-
convert_code_to_html(code, student_email): Converts the given code string to syntax-highlighted HTML, including the student's NetID.
-
write_pdf(html_content, output_file_path): Converts the given HTML content to a PDF file at the specified output path.
-
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.
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)
- 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.
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