You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

38 lines
1.0 KiB

#!/bin/python3
# Find all like <external-link href="https://www.duckduckgo.com" title="xyz">external resource</external-link>
# and convert to:
# <a href="https://www.duckduckgo.com" title="xyz" rel="external" target="_blank">external resource</a>
import sys
import json
import re
def ReplaceExternalLinkTagsInChapter(chapter):
# Easy-peasy-lemon-squeezy regex substitution
chapter['content'] = re.sub(
r'<external-link (.+?)>(.+?)</external-link>',
r'<a \1 rel="external" target="_blank">\2</a>',
chapter['content']
)
def Recursive_TraverseAllChapters(chapter):
ReplaceExternalLinkTagsInChapter(chapter)
for subsection in chapter['sub_items']:
Recursive_TraverseAllChapters(subsection['Chapter'])
if __name__ == '__main__':
if len(sys.argv) > 1:
if sys.argv[1] == "supports":
sys.exit(0)
context, book = json.load(sys.stdin)
for section in book['sections']:
Recursive_TraverseAllChapters(section['Chapter'])
print(json.dumps(book))