Skip to content

Commit

Permalink
Merge pull request #832 from googlefonts/html-formatter
Browse files Browse the repository at this point in the history
Format HTML files
  • Loading branch information
m4rc1e authored Feb 19, 2024
2 parents 339ddfa + 644c671 commit edbb47f
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
7 changes: 5 additions & 2 deletions Lib/gftools/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from gflanguages import LoadLanguages
from gftools.util import google_fonts as fonts
from gftools.gfgithub import GitHubClient
from gftools.utils import download_file
from gftools.utils import download_file, format_html

# ignore type because mypy error: Module 'google.protobuf' has no
# attribute 'text_format'
Expand Down Expand Up @@ -311,7 +311,10 @@ def _write_file_to_package(basedir:str, filename:str, data:bytes) -> None:

os.makedirs(os.path.dirname(full_name), exist_ok=True)
with open(full_name, 'wb') as f:
f.write(data)
if full_name.lower().endswith(".htm") or full_name.lower().endswith(".html"):
f.write(format_html(data).encode('utf-8'))
else:
f.write(data)

def _file_in_package(basedir, filename):
full_name = os.path.join(basedir, filename)
Expand Down
38 changes: 38 additions & 0 deletions Lib/gftools/scripts/format_html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# Copyright 2013 The Font Bakery Authors.
# Copyright 2017 The Google Font Tools Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# See AUTHORS.txt for the list of Authors and LICENSE.txt for the License.
#
import argparse
from gftools.utils import format_html

description = "Format HTML files according to Google Fonts specs"
parser = argparse.ArgumentParser(description=description)
parser.add_argument("html_file", nargs="+", help="HTML files to format")


def main(args=None):
args = parser.parse_args(args)
for path in args.html_file:
with open(path, "r", encoding="utf-8") as f:
content = f.read()
formatted = format_html(content)
with open(path, "w", encoding="utf-8") as f:
f.write(formatted)


if __name__ == "__main__":
main()
18 changes: 18 additions & 0 deletions Lib/gftools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from configparser import ConfigParser
else:
from ConfigParser import ConfigParser
from bs4 import BeautifulSoup

# =====================================
# HELPER FUNCTIONS
Expand Down Expand Up @@ -263,6 +264,23 @@ def mkdir(path, overwrite=True):
return path


def _html_custom_formatter(string):
whitespace = " "
# Remove new lines
string = string.replace("\n", " ")
# Remove extra spaces
string = " ".join(string.split())
# Break sentences into new lines
string = string.replace(". ", f".\n{whitespace}")
string = string.replace("! ", f"!\n{whitespace}")
string = string.replace("? ", f"?\n{whitespace}")
return string


def format_html(html):
return BeautifulSoup(html, "html.parser").prettify(formatter=_html_custom_formatter)


## Font-related utility functions

def font_stylename(ttFont):
Expand Down

0 comments on commit edbb47f

Please sign in to comment.