Skip to content

Commit

Permalink
Refactor, clarify process.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Max Carlson committed Feb 15, 2024
1 parent ccf30f9 commit b503bf3
Showing 1 changed file with 25 additions and 32 deletions.
57 changes: 25 additions & 32 deletions process.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,21 @@
import os
import re


# do regex substitution for each pattern across files with the specified ending in specified path
def updateFiles(patterns, path='.', ending='.html'):
for root, _, files in os.walk(path):
for file in files:
if file.endswith(ending):
filepath = os.path.join(root, file)
with open(filepath) as f:
s = f.read()
for pattern, replacement in patterns:
s = re.sub(pattern, replacement, s)
with open(filepath, "w") as f:
f.write(s)


# files to move to match existing site structure
files_to_move = [
(r'home-u40desktopu41-all-breakpoints.html', 'index.html'),
Expand All @@ -15,13 +30,8 @@
(r'background.html', '/background/index.html'),
]


# do string substitution for each pattern across all HTML files
directory = '.'

# URLs to replace
urlpatterns = [
# TODO: where should this go?
# (r"""https://about.publiccode.net/""", """https://staging.publiccode.net/our-mission"""),
(r"""https://projects.publiccode.net/""", """/resources-and-projects.html"""),
(r"""https://projects.publiccode.net""", """/resources-and-projects.html"""),
(r"""https://about.publiccode.net/CONTRIBUTING.html""", """/contributing.html"""),
Expand All @@ -30,13 +40,12 @@
(r' target="_blank"', ''),
(r"""https://publiccode.net/""", """/"""),
(r"""https://publiccode.net""", """/"""),
(r"""<link rel="stylesheet" type="text/css" href="css/""", """<link rel="stylesheet" type="text/css" href="/css/"""),
(r'" src="img/', '" src="/img/'),
]

urlpatterns += files_to_move

patterns = [
# HTML to replace
html_patterns = [
(r""" <!--<meta name=description content="This site was generated with Anima. www.animaapp.com"/>-->
<!-- <link rel="shortcut icon" type=image/png href="https://animaproject.s3.amazonaws.com/home/favicon.png" /> -->
<meta name="viewport" content="width=1440, maximum-scale=1.0" />
Expand All @@ -47,36 +56,20 @@
<link rel="icon" href="https://brand.publiccode.net/logo/mark-128w128h.png">
<script async defer data-domain="publiccode.net" src="https://plausible.io/js/plausible.js"></script>
</head>"""),
(r"""<link rel="stylesheet" type="text/css" href="css/""", """<link rel="stylesheet" type="text/css" href="/css/"""),
(r'" src="img/', '" src="/img/'),
]
patterns += urlpatterns
html_patterns += urlpatterns
updateFiles(html_patterns, '.', '.html')

for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.html'):
filepath = os.path.join(root, file)
with open(filepath) as f:
s = f.read()
for pattern, replacement in patterns:
s = re.sub(pattern, replacement, s)
with open(filepath, "w") as f:
f.write(s)

cssdirectory = './css/'
# process CSS
csspatterns = [
(r"""@import url\("https://px.animaapp.com/6406baa484a3afe9c63921de.6406baa605cc73851b593804.*.hcp.png"\);""", ''),
]
csspatterns += urlpatterns
# process css
for root, dirs, files in os.walk(cssdirectory):
for file in files:
if file.endswith('.css'):
filepath = os.path.join(root, file)
with open(filepath) as f:
s = f.read()
for pattern, replacement in csspatterns:
s = re.sub(pattern, replacement, s)
with open(filepath, "w") as f:
f.write(s)
updateFiles(csspatterns, './css/', '.css')


# Move files around to match the existing site
for old_filename, new_filename in files_to_move:
Expand Down

0 comments on commit b503bf3

Please sign in to comment.