Updates a Jekyll file to contain the counts form an object

    This just converts the results to YAML and adds to the Jekyll frontmatter.

    Args:
        filename: the Jekyll file to update
        result: the results object from `wc`
        content: the contents of the original file
        indent: the indentation level for dumping YAML

def update_file(filename, result, content, indent):
    """Updates a Jekyll file to contain the counts form an object

    This just converts the results to YAML and adds to the Jekyll frontmatter.

    Args:
        filename: the Jekyll file to update
        result: the results object from `wc`
        content: the contents of the original file
        indent: the indentation level for dumping YAML
    """
    # Split the file into frontmatter and content
    parts = re.split('---+', content, 2)

    # Load the frontmatter into an object
    frontmatter = yaml.safe_load(parts[1])

    # Add the counts entry in the results object to the frontmatter
    frontmatter['counts'] = result['counts']

    # Set the frontmatter part backed to the stringified version of the
    # frontmatter object
    parts[1] = '\n{}'.format(
        yaml.safe_dump(frontmatter, default_flow_style=False, indent=indent))
    result = '---'.join(parts)

    # Write everything back to the file
    with open(filename, 'wb') as f:
        f.write(result.encode('utf-8'))
    print('{} updated.'.format(filename))