示例#1
0
def check_tags(frontmatter):
    '''Checks tags in frontmatter against VALID_TAGS list.'''
    errCount = 0
    error_list = list()
    try:
        if not frontmatter['tags']:
            error_list.append(
                COLOR.colorize('WARNING: "tag" list empty in frontmatter',
                               'warn'))
        for tag in frontmatter['tags']:
            if tag not in VALID_TAGS:
                errCount += 1
                error_list.append(
                    COLOR.colorize('ERROR: {} is not a valid tag'.format(tag),
                                   'error'))
    except KeyError:
        errCount += 1
        error_list.append(
            COLOR.colorize('ERROR: "tag" key not found in frontmatter',
                           'error'))
    except Exception as e:
        errCount += 1
        error_list.append(COLOR.colorize('ERROR: {}'.format(e), 'error'))

    ERRPRINT(errCount, test_name='tags')

    return (errCount, error_list)
def main(ignore_list, extra_args):
    file_list = helpers.get_posts()
    html_strings = dict()
    non_centered = dict()
    errCount = 0
    if 'skip-html' not in extra_args:
        print(COLOR.colorize('Checking for errant HTML tags', 'info'), end=' ')
        for filename in file_list:
            (html_found, errs) = get_html(filename, ignore_list)
            html_strings = helpers.merge_dicts(html_strings, html_found)
            errCount += errs
        if errs > 0:
            print(PRINT_FAIL)
        for key, entry in html_strings.items():
            if entry:
                print('{}\n{}'.format(key, PRINT_ERROR))
                for element in entry:
                    print('{}: {}'.format(element[0], element[1]))
                print('')
        if errs == 0:
            print(PRINT_OK)

    if 'center-image' in extra_args:
        print(COLOR.colorize('\nVerifying images are centered', 'info'), end=' ')
        for filename in file_list:
            (found, errs) = check_image_centering(filename)
            non_centered = helpers.merge_dicts(found, non_centered)
            errCount += errs
        if errs > 0:
            print(PRINT_FAIL)
        for key, entry in non_centered.items():
            if entry:
                print('{}\n{}'.format(key, PRINT_ERROR))
                for element in entry:
                    print('{}: {}'.format(element[0], element[1]), end='')
        if errs == 0:
            print(PRINT_OK)

    if 'center-eqs' in extra_args:
        non_centered = dict()
        print(COLOR.colorize('\nVerifying equations are centered', 'info'), end=' ')
        for filename in file_list:
            (found, errs) = check_eq_centering(filename)
            non_centered = helpers.merge_dicts(found, non_centered)
            errCount += errs
        if errs > 0:
            print(PRINT_FAIL)
        for key, entry in non_centered.items():
            if entry:
                print('{}\n{}'.format(key, PRINT_ERROR))
                for element in entry:
                    print('{}: {}'.format(element[0], element[1], end=''))
        if errs == 0:
            print(PRINT_OK)

    print('\n')
    ERRPRINT(errCount, completion=True)

    return(0)
示例#3
0
def check_image_tags(filename, tag):
    errs = 0
    line_num = 0
    entries = list()
    with open(filename, 'r') as file:
        for line in file:
            line_num += 1
            if tag in line:
                errs += 1
                lnum = COLOR.colorize(str(line_num), 'linenum')
                entry = COLOR.colorize(line, 'error')
                entries.append([lnum, entry])
    return ({filename: entries}, errs)
示例#4
0
def check_eq_centering(filename):
    errs = 0
    line_num = 0
    entries = list()
    with open(filename, 'r') as file:
        for line in file:
            line_num += 1
            match = re.findall('^\+\+.*?\+\+\.$', line)
            if match and '<!-- lint-disable -->' not in line:
                errs += 1
                lnum = COLOR.colorize(str(line_num), 'linenum')
                entry = COLOR.colorize(line, 'error')
                entries.append([lnum, entry])
    return ({filename: entries}, errs)
示例#5
0
def get_frontmatter(filename, errCount):
    '''Extracts frontmatter from post for further processing.'''
    post_vars = dict()
    start_read = False
    errList = list()
    try:
        with open(filename, 'r') as file:
            for line in file:
                if line.strip() == '---' and start_read:
                    start_read = False
                    break

                if start_read:
                    line_split = line.split(':')
                    if line_split[0] == 'tags':
                        post_vars[line_split[0]] = list()
                    elif len(line_split) < 2 and 'tags' in post_vars.keys():
                        tag_name = line_split[0].split('-')
                        post_vars['tags'].append(tag_name[1].strip())
                    else:
                        post_vars[line_split[0]] = line_split[1].strip()

                if line.strip() == '---' and not start_read:
                    start_read = True
    except Exception as e:
        errCount += 1
        errList.append(COLOR.colorize("ERROR: {}".format(e), 'error'))

    return post_vars, errCount, errList
示例#6
0
def check_image_centering(filename):
    errs = 0
    line_num = 0
    entries = list()
    with open(filename, 'r') as file:
        last_line = ''
        for line in file:
            line_num += 1
            if 'site.image_path' in line:
                if last_line.strip() != '{: .center}':
                    errs += 1
                    lnum = COLOR.colorize(str(line_num), 'linenum')
                    entry = COLOR.colorize(line, 'error')
                    entries.append([lnum, entry])
            last_line = line

    return ({filename: entries}, errs)
示例#7
0
def check_old_eq_delim(filename):
    errs = 0
    line_num = 0
    entries = list()
    with open(filename, 'r') as file:
        for line in file:
            line_num += 1
            match1 = re.findall('^\$\$.*?\$\$\.$', line)
            match2 = re.findall('\$\$.*?\$\$', line)
            match3 = re.findall('\+\+.*?\$', line)
            match4 = re.findall('\$.*?\+\+', line)
            if match1 or match2 or match3 or match4:
                errs += 1
                lnum = COLOR.colorize(str(line_num), 'linenum')
                entry = COLOR.colorize(line, 'error')
                entries.append([lnum, entry])
    return ({filename: entries}, errs)
示例#8
0
def check_feature_key(frontmatter):
    '''Verifies feature_image set if the post is set to be featured.'''
    errCount = 0
    error_list = list()

    if 'project' in frontmatter.keys():
        try:
            is_project = frontmatter['project']
            is_featured = frontmatter['feature']
        except KeyError:
            is_featured = False
            error_list.append(
                COLOR.colorize('ERROR: "feature" key not found', 'error'))
            errCount += 1

        try:
            if is_featured == 'true':
                feature_image = frontmatter['feature_image']
        except KeyError:
            error_list.append(
                COLOR.colorize(
                    'ERROR: "feature_image" key not found and "feature" is set to {}'
                    .format(is_featured), 'error'))
            errCount += 1
        except IndexError:
            errCount += 1
            error_list.append(
                COLOR.colorize(
                    'ERROR: Marked as a project but the "feature" key is not set',
                    'error'))
        except Exception as e:
            errCount += 1
            error_list.append(COLOR.colorize('ERROR: {}'.format(e), 'error'))
    else:
        errCount += 1
        error_list.append(
            COLOR.colorize('ERROR: Missing "project" key', 'error'))

    ERRPRINT(errCount, test_name='feature_key')

    return (errCount, error_list)
示例#9
0
def main(extra_args):
    file_list = helpers.get_posts()
    errCount = 0
    tag = extra_args["tag"]
    skip_diff = extra_args["skip_diff"]
    print(COLOR.colorize(f"\nLooking for {tag}\n", "warn"))
    for filename in file_list:
        entries = find_tags(filename, tag)
        if not entries:
            msg = COLOR.colorize("No image tags found", "error")
            print(f"{filename}: {msg}")
            continue
        msg = COLOR.colorize(f"{len(entries)} tags found!", "warn")
        msg2 = COLOR.colorize(f" lines: {list(entries.keys())}", "linenum")
        print(f"{filename}: {msg} {msg2}")
        replace_tags(filename, entries)
        errCount += diff_before_continue(filename, skip_diff)

    print('\n')
    ERRPRINT(errCount, completion=True)
    return(errCount)
示例#10
0
def get_html(filename, ignore_list):
    '''Extracts html tags from file.'''
    html_strings = list()
    html_found = dict()
    line_number = 0
    errs = 0
    with open(filename, 'r') as file:
        for line in file:
            line_number += 1
            matches = re.findall('<.*?>', line)
            for m in matches:
                if any(tag in m for tag in ignore_list):
                    pass
                elif '<!-- lint-disable -->' in line:
                    pass
                else:
                    errs += 1
                    lnum = COLOR.colorize(str(line_number), 'linenum')
                    match = COLOR.colorize(m, 'error')
                    html_strings.append([lnum, match])

    html_found[filename] = html_strings
    return (html_found, errs)
示例#11
0
def diff_before_continue(filename, skip_diff):
    if not skip_diff:
        print("")
        os.system(f"diff --color {filename} {filename}.bak")
        print("")
        result = input(COLOR.colorize("OK to finalize changes? [y/N]", "warn"))
        print("")
        result_clean = result.lower()
    else:
        result_clean = "y"
    if result_clean == "y":
        os.system(f"mv {filename}.bak {filename}")
        return(0)
    else:
        os.system(f"rm {filename}.bak")
        return(1)
示例#12
0
def run_test(filename, test_name):
    '''Runs given test by first extracting frontmatter from file.'''
    errCount = 0
    frontmatter, errCount, errList = get_frontmatter(filename, errCount)
    if errCount > 0:
        return errCount, errList

    if test_name == 'feature-key':
        (test_err, error_list) = check_feature_key(frontmatter)
        errCount += test_err
    elif test_name == 'tags':
        (test_err, error_list) = check_tags(frontmatter)
        errCount += test_err
    else:
        print(COLOR.colorize('Unknown test "{}"'.format(test_name), 'error'))
        sys.exit(1)

    return (errCount, error_list)
示例#13
0
def main(test_args, skip_list):
    errCount = 0
    file_list = helpers.get_posts()
    for filename in file_list:
        if re.sub(helpers.PATH, '', filename) not in skip_list:
            test_array = list()
            PRINT_FILE_STRING = COLOR.colorize(filename, 'text')
            print(PRINT_FILE_STRING, end=" - ")
            test_err_list = list()
            for arg in test_args:
                (test_errs, test_err_list) = run_test(filename, arg)
                errCount += test_errs
                test_array.append(test_err_list)
            print('')
            for error in test_err_list:
                print(error)

    ERRPRINT(errCount, completion=True)

    return (0)
示例#14
0
def main(debug, checker=False):
    """Method to get list of tags."""
    all_posts = glob.glob("{}*.md".format(post_dir))
    all_tags = list()

    old_tags = glob.glob("{}*.md".format(tag_dir))

    if checker:
        errorCount = 0
        existing_tag_pages = list()
        for tag in old_tags:
            tagfile = os.path.basename(tag)
            tagname, ext = os.path.splitext(tagfile)
            existing_tag_pages.append(tagname)
        for valid_tag in VALID_TAGS:
            if valid_tag not in existing_tag_pages:
                print(
                    COLOR.colorize("Missing tag page {}".format(valid_tag),
                                   'error'))
                errorCount += 1
        ERRPRINT(errorCount, completion=True)
        sys.exit(0)
    else:
        for tag in old_tags:
            os.remove(tag)

        for tag in VALID_TAGS:
            tag_file = '{}{}.md'.format(tag_dir, tag)
            if debug:
                print("Generating {} tag page".format(tag_file))
            with open(tag_file, 'w') as ftag:
                out_str = "---\nlayout: tagpage\ntitle: \"Tag: {}\"\ntag: {}\nrobots: noindex\n---\n".format(
                    tag, tag)
                ftag.write(out_str)

        print('Generated {} Tags'.format(VALID_TAGS.__len__()))
示例#15
0
                    'ERROR: Marked as a project but the "feature" key is not set',
                    'error'))
        except Exception as e:
            errCount += 1
            error_list.append(COLOR.colorize('ERROR: {}'.format(e), 'error'))
    else:
        errCount += 1
        error_list.append(
            COLOR.colorize('ERROR: Missing "project" key', 'error'))

    ERRPRINT(errCount, test_name='feature_key')

    return (errCount, error_list)


if __name__ == '__main__':
    test_args = list()
    skip_list = list()
    if len(sys.argv) < 2:
        print(COLOR.colorize('ERROR: Requires test argument', 'error'))
        sys.exit(1)
    else:
        for arg in sys.argv[1:]:
            if '--skip=' in arg:
                comma_list = arg.split('--skip=')
                skip_list = comma_list[1].split(',')
            else:
                test_args.append(arg)

    main(test_args, skip_list)
示例#16
0
def main(ignore_list, extra_args):
    file_list = helpers.get_posts()
    html_strings = dict()
    non_centered = dict()
    found_tags = dict()
    errCount = 0
    if not extra_args['skip-html']:
        print(COLOR.colorize('Checking for errant HTML tags', 'info'), end=' ')
        errs_here = 0
        for filename in file_list:
            (html_found, errs) = get_html(filename, ignore_list)
            html_strings = helpers.merge_dicts(html_strings, html_found)
            errs_here += errs
            errCount += errs
        if errs_here > 0:
            print(PRINT_FAIL)
        for key, entry in html_strings.items():
            if entry:
                print('{}\n{}'.format(key, PRINT_ERROR))
                for element in entry:
                    print('{}: {}'.format(element[0], element[1]))
                print('')
        if errs_here == 0:
            print(PRINT_OK)

    if extra_args['img-tag']:
        tag = extra_args['img-tag']
        print(COLOR.colorize('\nVerifying all image tags have been updated',
                             'info'),
              end=' ')
        errs_here = 0
        for filename in file_list:
            (found, errs) = check_image_tags(filename, tag)
            found_tags = helpers.merge_dicts(found, found_tags)
            errs_here += errs
            errCount += errs
        if errs_here > 0:
            print(PRINT_FAIL)
        for key, entry in found_tags.items():
            if entry:
                print(f"{key}\n{PRINT_ERROR}")
                for element in entry:
                    print(f"{element[0]}: {element[1]}", end='')
        if errs_here == 0:
            print(PRINT_OK)
        else:
            print(COLOR.colorize('Must run ./script/gen_img!', 'warn'))

    if extra_args['center-image']:
        print(COLOR.colorize('\nVerifying images are centered', 'info'),
              end=' ')
        errs_here = 0
        for filename in file_list:
            (found, errs) = check_image_centering(filename)
            non_centered = helpers.merge_dicts(found, non_centered)
            errs_here += errs
            errCount += errs
        if errs_here > 0:
            print(PRINT_FAIL)
        for key, entry in non_centered.items():
            if entry:
                print('{}\n{}'.format(key, PRINT_ERROR))
                for element in entry:
                    print('{}: {}'.format(element[0], element[1]), end='')
        if errs_here == 0:
            print(PRINT_OK)

    if extra_args['center-eqs']:
        non_centered = dict()
        errs_here = 0
        print(COLOR.colorize('\nVerifying equations are centered', 'info'),
              end=' ')
        for filename in file_list:
            (found, errs) = check_eq_centering(filename)
            non_centered = helpers.merge_dicts(found, non_centered)
            errs_here += errs
            errCount += errs
        if errs_here > 0:
            print(PRINT_FAIL)
        for key, entry in non_centered.items():
            if entry:
                print('{}\n{}'.format(key, PRINT_ERROR))
                for element in entry:
                    print('{}: {}'.format(element[0], element[1], end=''))
        if errs_here == 0:
            print(PRINT_OK)

    if not extra_args['skip-eq-tags']:
        bad_tags = dict()
        errs_here = 0
        print(COLOR.colorize('\nVerifying equation tags', 'info'), end=' ')
        for filename in file_list:
            (found, errs) = check_old_eq_delim(filename)
            bad_tags = helpers.merge_dicts(found, bad_tags)
            errs_here += errs
            errCount += errs
        if errs_here > 0:
            print(PRINT_FAIL)
        for key, entry in bad_tags.items():
            if entry:
                print('{}\n{}'.format(key, PRINT_ERROR))
                for element in entry:
                    print('{}, '.format(element[0]), end='')
                print()
        if errs_here == 0:
            print(PRINT_OK)

    print('\n')
    ERRPRINT(errCount, completion=True)

    return (0)
示例#17
0
        print("")
        result = input(COLOR.colorize("OK to finalize changes? [y/N]", "warn"))
        print("")
        result_clean = result.lower()
    else:
        result_clean = "y"
    if result_clean == "y":
        os.system(f"mv {filename}.bak {filename}")
        return(0)
    else:
        os.system(f"rm {filename}.bak")
        return(1)

if __name__ == "__main__":
    extra_args = {}
    try:
        tag_index = sys.argv.index("--tag")
        tag = sys.argv[tag_index + 1]
    except IndexError:
        print(COLOR.colorize("Syntax error with custom tag.", "error"))
        sys.exit(1)
    except ValueError:
        tag = "[img]"
    extra_args["tag"] = tag
    extra_args["skip_diff"] = False

    if "--skip-diff" in sys.argv:
        extra_args["skip_diff"] = True

    sys.exit(main(extra_args))