3 ms·
Option #3 (HTML post-procssing) seemed to work. This Python script takes a directory of HTML files, and removes the [[ and ]] and replaces them with <span class
by dv35z 3y ago
Option #3 (HTML post-procssing) seemed to work. This Python script takes a directory of HTML files, and removes the [[ and ]] and replaces them with <span class="link-placeholder"> and </span>.
----
import os
from bs4 import BeautifulSoup
# Function to process HTML files in a directory
def process_html_files(directory):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.html'):
filepath = os.path.join(root, file)
process_html_file(filepath)
# Function to process individual HTML file
def process_html_file(filepath):
with open(filepath, 'r') as f:
html_content = f.read()
# Parse HTML
soup = BeautifulSoup(html_content, 'html.parser')
# Iterate through all text nodes in the HTML
for text_node in soup.find_all(text=True):
if '[[' in text_node and ']]' in text_node:
# Replace [[ and ]] within the text nodes with <span> tags
replaced_text = text_node.replace('[[', '<span class="link-placeholder">').replace(']]', '</span>')
text_node.replace_with(BeautifulSoup(replaced_text, 'html.parser'))
# Write back the modified HTML
with open(filepath, 'w') as f:
f.write(str(soup))
# Replace 'your_directory_path' with the directory containing your HTML files
process_html_files('site')