Skip to content
This repository has been archived by the owner on Dec 30, 2021. It is now read-only.

Commit

Permalink
fix: Fix admonition and code blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Nov 11, 2021
1 parent 258a2de commit e29fee1
Showing 1 changed file with 63 additions and 41 deletions.
104 changes: 63 additions & 41 deletions YAFPA/common/admonition.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
from itertools import zip_longest
from pathlib import Path

import yaml
Expand All @@ -8,6 +9,25 @@

BASEDIR = Path(settings.BASEDIR)

def code_blocks(start_list, end_list):
start_bug = []
end_bug = []
bug = [(x, y) for x, y in zip_longest(start_list, end_list, fillvalue=-1)]
for i in bug:
if i[0] > i[1]:
start_bug.append(i[0])
elif i[0] == -1:
end_bug.append(i[1])
merged = []
for i in start_bug:
for j in end_bug:
if i < j:
merged.append((i, j))
no_bug = [(x, y) for x, y in zip_longest(start_list, end_list, fillvalue=-1) if
x != -1 and x < y]
merged = no_bug + merged
return merged

def admonition_logo(type, line):
admonition = {
"note": "🖊️",
Expand Down Expand Up @@ -73,7 +93,7 @@ def admonition_trad_content(line, type):
title = line
if re.search("[*\-\+] ", line):
title = re.sub("[*\-\+] ", "**•**{: .bullet} ", line)
if "collapse:" in line:
elif "collapse:" in line:
title = ""
elif "icon:" in line:
title = ""
Expand Down Expand Up @@ -124,59 +144,61 @@ def admonition_trad(file_data):
"cite",
]
for i in range(0, len(file_data)):
if re.search("[`?!]{3}( ?)ad-(.*)", file_data[i]):
if re.search("[`?!]{3}( ?)\w+(.*)", file_data[i]):
start = i
start_list.append(start)
elif re.match("```", file_data[i]) or re.match("--- admonition", file_data[i]):
if re.match("^```$", file_data[i]) or re.match("--- admonition", file_data[i]) :
end = i
end_list.append(end)
for i, j in zip(start_list, end_list):
merged = code_blocks(start_list, end_list)
for i, j in merged:
code = {code_index: (i, j)}
code_index = code_index + 1
code_dict.update(code)

offset_for_title = 0
for ad, ln in code_dict.items():
ad_start = ln[0] + offset_for_title
ad_end = ln[1] + offset_for_title
type = re.search("[`!?]{3}( ?)ad-\w+", file_data[ad_start])
type = re.sub("[`!?]{3}( ?)ad-", "", type.group())
adm = "b"
title = ""
if re.search("[!?]{3} ad-(\w+) (.*)", file_data[ad_start]):
title = re.search("[!?]{3} ad-(\w+) (.*)", file_data[ad_start])
adm = "MT"
title = title.group(2)
first_block = re.search("ad-(\w+)", file_data[ad_start])
adm_type_code = first_block.group().replace("ad-", "")
if adm_type_code not in adm_list:
first_block = "ad-note"
else:
first_block = first_block.group()
first_block = " \n!!!" + first_block + " "

num_lines = lambda x: x.count("\n")

file_data[ad_start] = re.sub(
"[`!?]{3}( ?)ad-(.*)", first_block, file_data[ad_start]
)

file_data[ad_end] = " \n"
for i in range(ad_start, ad_end):
file_data[i] = admonition_trad_content(file_data[i], type)

if adm == "MT":
if len(title) > 0:
title_admo = admonition_logo(type, title)
if type:
type = re.sub("[`!?]{3}( ?)ad-", "", type.group())
adm = "b"
if re.search("[!?]{3} ad-(\w+) (.*)", file_data[ad_start]):
title = re.search("[!?]{3} ad-(\w+) (.*)", file_data[ad_start])
adm = "MT"
title = title.group(2)
first_block = re.search("ad-(\w+)", file_data[ad_start])
if first_block:
file_data[ad_end] = ' \n'
adm_type_code = first_block.group().replace("ad-", "")
if adm_type_code not in adm_list:
first_block = "ad-note"
else:
title_admo = admonition_logo(type, "")

file_data.insert(ad_start + 1, title_admo)
offset_for_title += 1
else:
converted = [file_data[i] for i in range(ad_start, ad_end)]
if not any(re.search(".*title.*", line) for line in converted):
title_admo = admonition_logo(type, "")
first_block = first_block.group()
first_block = " \n!!!" + first_block + " "

num_lines = lambda x: x.count("\n")

file_data[ad_start] = re.sub(
"[`!?]{3}( ?)ad-(.*)", first_block, file_data[ad_start]
)


for i in range(ad_start, ad_end):
file_data[i] = admonition_trad_content(file_data[i], type)

if adm == "MT":
if len(title) > 0:
title_admo = admonition_logo(type, title)
else:
title_admo = admonition_logo(type, "")

file_data.insert(ad_start + 1, title_admo)
offset_for_title += 1
else:
converted = [file_data[i] for i in range(ad_start, ad_end)]
if not any(re.search(".*title.*", line) for line in converted):
title_admo = admonition_logo(type, "")
file_data.insert(ad_start + 1, title_admo)
offset_for_title += 1
return file_data

0 comments on commit e29fee1

Please sign in to comment.