"""functions to add css to html""" #import os import sys import lxml.etree as etree from email.utils import formataddr #from email.mime.text import MIMEText #from email.header import Header #from email.mime.multipart import MIMEMultipart import re #import subprocess import filing from nitpo import Nitpo from sheets import Sheets from xpaths import Xpaths import docing class Inces(Nitpo): def __init__(self, do_verbose=False): super().__init__() self.sheets = Sheets() self.do_verbose = do_verbose self.css_doc = self.load_css() ## parse for variable substitution self.x = Xpaths() self.var = self.get_var() self.res = self.get_res() self.read_locs() def load_css(self): if not self.has_conf('files', 'css_xml'): print("inces needs ะค[files][css_xml]") quit() css_fufi = self.conf['files']['css_xml'] css_doc = filing.parse_lax(css_fufi) return css_doc ## for legacy, change the namepage manually, and reparse #string = docing.show(css_doc) ### I know this is ugly but temprorary #string = string.replace('', # '') #css_doc = etree.fromstring(string) ## print(docing.show(doc)) #return css_doc def trans_fufi(self, fufi): if not fufi.endswith('.xhtml'): print("inces needs .xhtml input", file=sys.stderr) quit() def get_var(self): xp = '/n:rules/n:var' var_eles = self.x.run(self.css_doc, xp) var = {} for var_ele in var_eles: if 'name' not in var_ele.attrib: err = docing.show(var_ele) print("inces: no name for variable {err}", file=sys.stderr) continue if 'value' not in var_ele.attrib: err = docing.show(var_ele) print("inces: no value for variable {err}", file=sys.stderr) continue var[var_ele.attrib['name']] = var_ele.attrib['value'] return var def get_res(self): res = {} ## simple class res['class'] = re.compile(r'\.[a-z_]+$') res['id'] = re.compile(r'#[a-z_]+$') res['name'] = re.compile(r'[a-z]([a-z]+|[0-9])$') return res def read_locs(self): xp = '/n:rules/n:rule/n:loc' # # we don't use /n:rules/n:media, should be obsoleted anyway loc_eles = self.x.run(self.css_doc, xp) locs = {} for loc_ele in loc_eles: loc = loc_ele.text found = False for name in self.res: if self.res[name].match(loc): found = True if name not in locs: locs[name] = {} rules = self.get_rules(loc) locs[name][loc] = rules break if not found and self.do_verbose: print(f"inces can not implement '{loc}'") continue print('class') print(locs['class']) print('id') print(locs['id']) print('name') print(locs['name']) def get_rules(self, loc): xp = f"/n:rules/n:rule/n:loc[text()='{loc}']" rule_eles = self.x.run(self.css_doc, xp) if len(rule_eles) == 0: if self.do_verbose: print(f"inces can not find a rule for {loc}") return None xp = '../n:prop' rules = {} for rule_ele in rule_eles: prop_eles = self.x.run(rule_ele, xp) for prop_ele in prop_eles: if 'name' not in prop_ele.attrib: err = docing.show(prop_ele) print("inces: no name for variable {err}", file=sys.stderr) continue name = prop_ele.attrib['name'] if name in rules: if self.do_verbose: print(f"inces find duplicate rule {name} in {loc}") else: rules[name] = '' ## normal case if 'lit' in prop_ele.attrib: rules[name] += prop_ele.attrib['lit'] continue ## no @lit, no @var if 'var' not in prop_ele.attrib: if self.do_verbose: print("inces sees neither lit= nor var= in {name} at {loc}") continue ## var case var = prop_ele.attrib['var'] if var not in self.var: if self.do_verbose: print("inces sees refer error for {name} in {loc}") continue rules[name] += self.var[var] out = self.format_rules(rules) return out def format_rules(self, rules): out = '' for name in rules: out += name + ': ' + rules[name] + '; ' out = out[:-2] return out