Skip to content

Instantly share code, notes, and snippets.

@Destaq
Created October 4, 2022 18:54
Show Gist options
  • Save Destaq/725aa2b8af1efa07a76cc056c8f063ca to your computer and use it in GitHub Desktop.
Save Destaq/725aa2b8af1efa07a76cc056c8f063ca to your computer and use it in GitHub Desktop.
Generate an SVG representation of a xianxia or xuanhuan cultivator's progress throughout a story by analyzing their book file
import random
import re
WEBNOVEL_SUBPATH = "《凡人修仙传》.txt"
WEBNOVEL_ENGLISH = "A Record of a Mortal's Journey to Immortality"
LEVELS = ["炼气", "筑基", "结丹", "元婴", "化神", "炼虚", "合体", "大乘"]
LEVELS_ENG = ["Qi Condensation", "Foundation Establishment", "Core Formation", "Nascent Soul", "Deity Transformation",
"Spatial Tempering", "Body Integration", "Grand Ascension"]
#####
# FURTHER EXAMPLES
#####
# WEBNOVEL_SUBPATH = "《修真聊天群》.txt"
# WEBNOVEL_ENGLISH = "Cultivation Chat Group"
# LEVELS = ["一品", "二品", "三品", "四品", "五品", "六品", "七品", "八品", "九品", "长生者"]
# LEVELS_ALT = ["跃凡", "真师", "战王", "武皇", "灵皇", "真君", "灵尊", "玄圣", "劫仙", "长生者"]
# LEVELS_ENG = ["First Stage", "Second Stage", "Third Stage", "Fourth Stage", "Fifth Stage", "Sixth Stage", "Seventh Stage", "Eighth Stage", "Ninth Stage", "Immortal Being"]
# WEBNOVEL_SUBPATH = "一念永恒.txt"
# WEBNOVEL_ENGLISH = "A Will Eternal"
# LEVELS = ["凝气", "筑基", "结丹", "元婴", "天人", "半神", "大乘", "太古", "主宰", "永恒"]
# LEVELS_ENG = ["Qi Condensation", "Foundation Establishment", "Core Formation", "Nascent Soul", "Deva", "Demigod", "Celestial", "Archaean", "Sovereign", "Eternal"]
# WEBNOVEL_SUBPATH = "盘龙.txt"
# WEBNOVEL_ENGLISH = "Coiling Dragon"
# LEVELS = ["魔法师", "圣域", "下位神", "中位神", "上位神", "七级恶魔", "统领", "主神", "至高神"]
# LEVELS_ENG = ["Magus", "Saint", "Lesser God", "Middle God", "High God", "Seventh Level Demon", "Leader", "Sovereign God", "Plane God"]
# WEBNOVEL_SUBPATH = "我欲封天.txt"
# WEBNOVEL_ENGLISH = "I Shall Seal the Heavens"
# LEVELS = ["凝气", "筑基", "结丹", "元婴", "斩灵", "问道", "真仙", "古境", "道境", "主宰", "祖境"]
# LEVELS_ENG = ["Qi Condensation", "Foundation Establishment", "Core Formation", "Nascent Soul", "Spirit Severing", "Dao Seeking", "True Immortal", "Ancient Realm", "Daorealm", "Daosource/Sovereign", "Ancestor Realm"]
LEVELS_LOCATIONS = [] # stores tuples of color and percentage location
SVG_COLORS_LIST = [
"red",
"orange",
"gold",
"green",
"aqua",
"blue",
"fuchsia",
"purple",
"moccasin",
"black",
"yellow"
]
file = open(f"data/{WEBNOVEL_SUBPATH}", "r").read()
def shorten(label):
if len(label) > 18:
return label[:18] + "..."
else:
return label
# find all the locations of the level strings in the file
for level in LEVELS:
# done with regex
for m in re.finditer(level, file):
LEVELS_LOCATIONS.append((level, round(m.start() / len(file) * 100 / 1.05, 2)))
random.shuffle(LEVELS_LOCATIONS)
# remove duplicates
LEVELS_LOCATIONS = list(dict.fromkeys(LEVELS_LOCATIONS)) # required as too-close rects overwrite each other
# now create a svg file that shows the locations of the levels
svg = open("result.svg", "w")
svg.write(
f"""<svg xmlns="http://www.w3.org/2000/svg" width="40%" height="100%" viewBox="0 0 40 100" style="border:1px
dotted black"> """
)
# write the name of the book at the beginning
svg.write(
f"""<text id="noveltitle" x="50%" y="2%" font-size="15%" fill="black" dominant-baseline="middle"
text-anchor="middle" font-weight="bold" text-decoration="underline">{WEBNOVEL_ENGLISH.split(".")[0]}</text>
"""
)
# and add a legend label
svg.write(
f"""<text id="noveltitle" x="95%" y="6.5%" font-size="13%" fill="black" font-style="italic">Legend</text>
"""
)
for level in LEVELS:
# add in black level descriptor
svg.write(
f"""<text x="70%" y="{LEVELS.index(level) * 3 + 9}" font-size="13%" fill="black">• {shorten(LEVELS_ENG[LEVELS.index(level)])}</text> """
)
# write colored level descriptor
svg.write(
f"""<text x="120%" y="{LEVELS.index(level) * 3 + 9}" font-size="13%" font-style="italic" fill="{SVG_COLORS_LIST[LEVELS.index(level)]}">{SVG_COLORS_LIST[LEVELS.index(level)]}</text> """
)
# draw in individual lines
for location in LEVELS_LOCATIONS:
svg.write(
f"""<rect x="-35%" y="{location[1] + 4}%" width="100%" height="0.05" fill="{SVG_COLORS_LIST[LEVELS.index(location[0])]}" /> """
)
svg.write("</svg>")
svg.close()
print("Process complete, find image at result.svg.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment