From 9f4806990bf924d95656c6fe5e3efc9e10665a85 Mon Sep 17 00:00:00 2001 From: Libo Su Date: Thu, 21 Jul 2022 23:03:21 +0200 Subject: [PATCH] add file name as title in pdf --- Code2pdf/code2pdf.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Code2pdf/code2pdf.py b/Code2pdf/code2pdf.py index b2db891..c4ac894 100755 --- a/Code2pdf/code2pdf.py +++ b/Code2pdf/code2pdf.py @@ -7,6 +7,7 @@ import os import re import sys +from pathlib import Path try: import pygments @@ -71,7 +72,14 @@ def highlight_file(self, linenos=True, style='default'): logging.error(fmt.format(self.input_file, exread)) sys.exit(2) - return pygments.highlight(content, lexer, formatter) + # use file name as title + doc_html = pygments.highlight(content, lexer, formatter) + file_name = Path(self.input_file).name + title_tag = "

" + idx = doc_html.index(title_tag) + len(title_tag) + doc_html = doc_html[:idx] + '\n' + file_name + '\n' + doc_html[idx:] + + return doc_html def init_print(self, linenos=True, style="default"): app = QApplication([]) # noqa