MarcusNyne efa87a6abe Display features for PNG Info tab
Text on PNG Info tab is parsed and colorized in a way that makes it easier to read.  Parameter values can be copied by clicking on them.
- High performance regex used for parsing
- Normal values are displayed in blue, but string content is displayed in orange to improve readability (i.e. adetailer prompts)
- Clicking to copy uses a pointer cursor and a green color animation to show something is happening
- Color choices configured differently for dark mode in order to optimize readability
- Copying strings with \n automatically converts to newlines during copy operation
- Settings that don't follow normal conventions are not parsed, but displayed in the old style (i.e. dynamic prompt templates)
- If there are parsing issues (or exceptions), defaults to the old display mode
2024-08-19 17:26:56 -04:00

49 lines
1.7 KiB
Python

import re
class PngParser:
re_top_level = None
re_extra_newline = None
re_parameters = None
def __init__(self, pnginfo_string):
PngParser.init_re()
self.valid = self.parse_pnginfo(pnginfo_string)
def parse_pnginfo(self, pnginfo_string):
try:
# separate positive, negative, and parameters
tlen = len(pnginfo_string)
m = PngParser.re_top_level.search(pnginfo_string)
if m is None:
return False
self.positive = m.group(1)
self.negative = m.group(2)
self.parameters = m.group(3)
self.extra = None
self.settings = None
# parse extra parameters (if they exist) by a newline outside of quotes
m = PngParser.re_extra_newline.search(self.parameters)
if m is not None:
s = m.span()
self.extra = self.parameters[s[1]:]
self.parameters = self.parameters[:s[0]]
# parse standard parameters
self.settings = PngParser.re_parameters.findall(self.parameters)
if self.settings is None:
return False
except:
return False
return True
@classmethod
def init_re(cls):
if cls.re_top_level is None:
cls.re_top_level = re.compile(r'^(?P<positive>(?:.|\n)*)\nNegative prompt: (?P<negative>(?:.|\n)*)\n(?=Steps: )(?P<parameters>(?:.|\n)*)$')
cls.re_extra_newline = re.compile(r'\n(?=(?:[^"]*"[^"]*")*[^"]*$)')
cls.re_parameters = re.compile(r'\s*(?P<param>[^:,]+):\s*(?P<quote>")?(?P<value>(?(2)(?:.)*?(?:(?<!\\)")|.*?))(?:\s*,\s*|$)')