Skip to content

Instantly share code, notes, and snippets.

@fooker
Created October 19, 2023 13:39
Show Gist options
  • Save fooker/3097e401069e9c6db056efe98133c146 to your computer and use it in GitHub Desktop.
Save fooker/3097e401069e9c6db056efe98133c146 to your computer and use it in GitHub Desktop.
jsp-trans.py
from pathlib import Path
from collections import defaultdict
import re
from bs4 import BeautifulSoup
pattern = re.compile('<jsp:include page="/includes/bootstrap\.jsp" flush="false"\s*>(.*?)</jsp:include>', flags=re.MULTILINE|re.DOTALL)
allFlags = set()
for path in Path('opennms-webapp/src').rglob('*.jsp'):
with path.open('r') as f:
content = f.read()
def repl(match):
result = f'<%@ page import="org.opennms.web.utils.Bootstrap" %>\n'
xml = BeautifulSoup(match.group(0), 'html.parser')
params = defaultdict(list)
for param in xml.find_all('jsp:param'):
params[param['name']].append(param['value'])
params.pop('title', None)
params.pop('location', None)
params.pop('meta', None)
params.pop('vaadinEmbeddedStyles', None)
params.pop('norequirejs', None)
params.pop('storageAdmin', None)
scrollSpy = params.pop('scrollSpy', [None])[0]
ngApp = params.pop('ngapp', [None])[0]
headTitles = params.pop('headTitle', [])
breadcrumbs = params.pop('breadcrumb', [])
allFlags.update(params.keys())
flags = set()
for (k, v) in params.items():
if v == ['true']:
flags.add(k)
else:
print('Not a flag: ', k, v)
flags = map(lambda s: f'"{s}"', flags)
flags = ', '.join(flags)
def val(value):
if value.startswith('<%=') and value.endswith('%>'):
value = value.removeprefix('<%=')
value = value.removesuffix('%>')
value = value.strip()
return value
if value.startswith('<a') and value.endswith('a>'):
a = BeautifulSoup(value, 'html.parser').find('a')
title = a.text
link = a['href']
return f'"{title}", "{link}"'
value = f'"{value}"'
# value = re.sub(r'\$\{(.+?)\}', r'" + \1 + "', value)
# value = value.removeprefix('"" + ')
# value = value.removesuffix(' + ""')
return value
result += f'<% Bootstrap.with(pageContext)\n'
for headTitle in headTitles:
result += f' .headTitle({val(headTitle)})\n'
for breadcrumb in breadcrumbs:
result += f' .breadcrumb({val(breadcrumb)})\n'
if scrollSpy is not None:
result += f' .scrollSpy({val(scrollSpy)})\n'
if ngApp is not None:
result += f' .ngApp({val(ngApp)})\n'
if flags:
result += f' .flags({flags})\n'
result += f' .build(request);\n'
result += f'%>\n'
result += f'<jsp:directive.include file="/includes/bootstrap.jsp" />'
return result
content = pattern.sub(repl, content)
with path.with_suffix('.jsp').open('w') as f:
f.write(content)
print(allFlags)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment