Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to specify encoding of files #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions htmlmerger/htmlmerger.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(
] = None,
input_directory: Union[Path, str] = None,
output_file: Union[Path, str] = Path("merged.html"),
encoding: str = None
):
"""
Parameters
Expand All @@ -53,10 +54,14 @@ def __init__(
Directory containing html files to merge. Alternative to "files" (default value = None).
output_file: Union[Path, str]
File in which to save the merged html. (default value = "./merged.html").
encoding: str
Encoding is the name of the encoding used to decode or encode the files.
The default encoding is platform dependent. (default value = None)
"""
self.files = files
self.input_directory = input_directory
self.output_file = output_file
self.encoding = encoding
self.header = ""
self.tail = ""
self.contents = {}
Expand Down Expand Up @@ -93,7 +98,7 @@ def check_args(self):
def get_contents(self):
first = True
for file in self.files:
for line in file.read_text().splitlines():
for line in file.read_text(encoding=self.encoding).splitlines():
if line.startswith("<html>") or line.startswith("<body>") or line.startswith("<head>"):
if first:
if self.header == "":
Expand Down Expand Up @@ -122,7 +127,7 @@ def merge(self, clean: bool = False):

if not self.loaded:
self.get_contents()
with open(self.output_file, "w") as ofile:
with open(self.output_file, "w", encoding=self.encoding) as ofile:
ofile.write(f"{self.header}\n")
for name in self.contents:
ofile.write(f"{self.contents[name]}\n")
Expand Down