# Buggy code
def read_metadata(
    self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None
):
    """Read the metadata from a post's meta tags, and return a metadata dict."""
    if lang is None:
        lang = LocaleBorg().current_lang
    source_path = post.translated_source_path(lang)

    with io.open(source_path, "r", encoding="utf-8") as inf:
        data = inf.read()

    metadata = {}
    doc = lxml.html.document_fromstring(data)
    title_tag = doc.find("*//title")
    if title_tag is not None:
        metadata["title"] = title_tag.text
    meta_tags = doc.findall("*//meta")
    for tag in meta_tags:
        k = tag.get("name", "").lower()
        if not k:
            continue
        elif k == "keywords":
            k = "tags"
        metadata[k] = tag.get("content", "")
    map_metadata(metadata, "html_metadata", self.site.config)
    return metadata


# Traceback (lxml.etree.ParserError)
Scanning posts.......[2017-06-21T23:15:16Z] ERROR: scan_posts: Error reading post pages/index.html
[2017-06-21T23:15:16Z] ERROR: Nikola: Error reading timeline
[2017-06-21T23:15:16Z] ERROR: Nikola: Error loading tasks. An unhandled exception occurred.
Traceback (most recent call last):
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/doit/doit_cmd.py", line 167, in run
return command.parse_execute(args)
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/doit/cmd_base.py", line 120, in parse_execute
return self.execute(params, args)
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/doit/cmd_base.py", line 378, in execute
self, params, args)
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/__main__.py", line 286, in load_tasks
self.nikola.gen_tasks('render_site', "Task", 'Group of tasks to render the site.'))
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/doit/loader.py", line 313, in generate_tasks
for task_dict, x_doc in flat_generator(gen_result, gen_doc):
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/doit/loader.py", line 26, in flat_generator
for item in gen:
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/nikola.py", line 2059, in gen_tasks
for task in flatten(pluginInfo.plugin_object.gen_tasks()):
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/nikola.py", line 2053, in flatten
for t in task:
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/plugins/task/sources.py", line 49, in gen_tasks
self.site.scan_posts()
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/nikola.py", line 2178, in scan_posts
timeline = p.plugin_object.scan()
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/plugins/misc/scan_posts.py", line 101, in scan
destination_base=destination_translatable
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/post.py", line 162, in __init__
default_metadata, self.newstylemeta = get_meta(self, self.config['FILE_METADATA_REGEXP'], self.config['UNSLUGIFY_TITLES'])
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/post.py", line 1155, in get_meta
compiler_meta = post.compiler.read_metadata(post, file_metadata_regexp, unslugify_titles, lang)
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/nikola/plugins/compile/html.py", line 100, in read_metadata
doc = lxml.html.document_fromstring(data)
File "/Users/christianbarra/miniconda3/envs/www.cassiny.io/lib/python3.6/site-packages/lxml/html/__init__.py", line 765, in document_fromstring
"Document is empty")
lxml.etree.ParserError: Document is empty

# Fixed code
def read_metadata(
    self, post, file_metadata_regexp=None, unslugify_titles=False, lang=None
):
    """Read the metadata from a post's meta tags, and return a metadata dict."""
    if lang is None:
        lang = LocaleBorg().current_lang
    source_path = post.translated_source_path(lang)

    with io.open(source_path, "r", encoding="utf-8") as inf:
        data = inf.read()

    metadata = {}
    try:
        doc = lxml.html.document_fromstring(data)
    except lxml.etree.ParserError as e:
        # Issue #374 -> #2851
        if str(e) == "Document is empty":
            return {}
        # let other errors raise
        raise
    title_tag = doc.find("*//title")
    if title_tag is not None:
        metadata["title"] = title_tag.text
    meta_tags = doc.findall("*//meta")
    for tag in meta_tags:
        k = tag.get("name", "").lower()
        if not k:
            continue
        elif k == "keywords":
            k = "tags"
        metadata[k] = tag.get("content", "")
    map_metadata(metadata, "html_metadata", self.site.config)
    return metadata