import os import re import sys import argparse parser = argparse.ArgumentParser(description=\ '''Convert the W3C CSS 2.1 test suite to RML documents for testing in RmlUi. Fetch the CSS tests archive from here: https://www.w3.org/Style/CSS/Test/CSS2.1/ Extract the 'xhtml1' folder and point the 'in_dir' argument to this directory.''') parser.add_argument('in_dir', help="Input directory which contains the 'xhtml1' (.xht) files to be converted.") parser.add_argument('out_dir', help="Output directory for the converted RML files.") parser.add_argument('--clean', action='store_true', help='Will *delete* all existing *.rml files in the output directory.') parser.add_argument('--match', help="Only process file names containing the given string.") args = parser.parse_args() in_dir = args.in_dir out_dir = args.out_dir out_ref_dir = os.path.join(out_dir, r'reference') match_files = args.match if not os.path.isdir(in_dir): print("Error: Specified input directory '{}' does not exist.".format(out_dir)) exit() if not os.path.exists(out_dir): try: os.mkdir(out_dir) except Exception as e: print('Error: Failed to create output directory {}'.format(out_dir)) if not os.path.exists(out_ref_dir): try: os.mkdir(out_ref_dir) except Exception as e: print('Error: Failed to create reference output directory {}'.format(out_ref_dir)) if not os.path.isdir(out_dir) or not os.path.isdir(out_ref_dir): print("Error: Specified output directory '{}' or reference '{}' are not directories.".format(out_dir, out_ref_dir)) exit() if args.clean: print("Deleting all *.rml files in output directory '{}' and reference directory '{}'".format(out_dir, out_ref_dir)) for del_dir in [out_dir, out_ref_dir]: for file in os.listdir(del_dir): path = os.path.join(del_dir, file) try: if os.path.isfile(path) and file.endswith('.rml'): os.unlink(path) except Exception as e: print('Failed to delete {}. Reason: {}'.format(path, e)) reference_links = [] def process_file(in_file): in_path = os.path.join(in_dir, in_file) out_file = os.path.splitext(in_file)[0] + '.rml' out_path = os.path.join(out_dir, out_file) f = open(in_path, 'r', encoding="utf8") lines = f.readlines() f.close() data = '' reference_link = '' in_style = False for line in lines: if re.search(r')', r'()', ] for reference_link_search in reference_link_search_candidates: reference_link_match = re.search(reference_link_search, line, flags = re.IGNORECASE) if reference_link_match: reference_link = reference_link_match[2] + '.xht' line = re.sub(reference_link_search, r'\1.rml\3', line, flags = re.IGNORECASE) break line = re.sub(r']*>\s*', '', line, flags = re.IGNORECASE) line = re.sub(r' xmlns="[^"]+"', '', line, flags = re.IGNORECASE) line = re.sub(r'<(/?)html[^>]*>', r'<\1rml>', line, flags = re.IGNORECASE) line = re.sub(r'^(\s*)(.*
]*>)', r'\1\2\n\1\1', line, flags = re.IGNORECASE) line = re.sub(r'direction:\s*ltr\s*;?', r'', line, flags = re.IGNORECASE) line = re.sub(r'list-style(-type)?:\s*none\s*;?', r'', line, flags = re.IGNORECASE) line = re.sub(r'max-height:\s*none;', r'max-height: -1px;', line, flags = re.IGNORECASE) line = re.sub(r'max-width:\s*none;', r'max-width: -1px;', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*xxx-large', r'\1 2.0em', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*xx-large', r'\1 1.7em', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*x-large', r'\1 1.3em', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*large', r'\1 1.15em', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*medium', r'\1 1.0em', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*small', r'\1 0.9em', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*x-small', r'\1 0.7em', line, flags = re.IGNORECASE) line = re.sub(r'(font-size:)\s*xx-small', r'\1 0.5em', line, flags = re.IGNORECASE) line = re.sub(r'(line-height:)\s*normal', r'\1 1.2em', line, flags = re.IGNORECASE) line = re.sub(r'cyan', r'aqua', line, flags = re.IGNORECASE) if re.search(r'background:[^;}\"]*fixed', line, flags = re.IGNORECASE): print("File '{}' skipped since it uses unsupported background.".format(in_file)) return False line = re.sub(r'background:(\s*([a-z]+|#[0-9a-f]+)\s*[;}\"])', r'background-color:\1', line, flags = re.IGNORECASE) # Try to fix up borders to match the RmlUi syntax. This conversion might ruin some tests. line = re.sub(r'(border(-(top|right|bottom|left))?)-style:\s*solid(\s*[;}"])', r'\1-width: 3px\4', line, flags = re.IGNORECASE) line = re.sub(r'(border(-(top|right|bottom|left))?):\s*none(\s*[;}"])', r'\1-width: 0px\4', line, flags = re.IGNORECASE) line = re.sub(r'(border[^:]*:[^;]*)thin', r'\1 1px', line, flags = re.IGNORECASE) line = re.sub(r'(border[^:]*:[^;]*)medium', r'\1 3px', line, flags = re.IGNORECASE) line = re.sub(r'(border[^:]*:[^;]*)thick', r'\1 5px', line, flags = re.IGNORECASE) line = re.sub(r'(border[^:]*:[^;]*)none', r'\1 0px', line, flags = re.IGNORECASE) line = re.sub(r'(border[^:]*:\s*[0-9][^\s;}]*)\s+soli?d', r'\1 ', line, flags = re.IGNORECASE) line = re.sub(r'(border[^:]*:\s*[^0-9;}]*)soli?d', r'\1 3px', line, flags = re.IGNORECASE) if re.search(r'border[^;]*(hidden|dotted|dashed|double|groove|ridge|inset|outset)', line, flags = re.IGNORECASE) \ or re.search(r'border[^:]*-style:', line, flags = re.IGNORECASE): print("File '{}' skipped since it uses unsupported border styles.".format(in_file)) return False line = re.sub(r'(border(-(top|right|bottom|left))?:\s*)[0-9][^\s;}]*(\s+[0-9][^\s;}]*[;}])', r'\1 \4', line, flags = re.IGNORECASE) line = re.sub(r'(border(-(top|right|bottom|left))?:\s*[0-9\.]+[a-z]+\s+)[0-9\.]+[a-z]+([^;}]*[;}])', r'\1 \4', line, flags = re.IGNORECASE) line = re.sub(r'(border(-(top|right|bottom|left))?:\s*[0-9\.]+[a-z]+\s+)[0-9\.]+[a-z]+([^;}]*[;}])', r'\1 \4', line, flags = re.IGNORECASE) line = re.sub(r'(border:)[^;]*none([^;]*;)', r'\1 0px \2', line, flags = re.IGNORECASE) if in_style and not '<' in line: line = line.replace('>', '>') flags_match = re.search(r'