Un Verificador de links simple para Nikola
Este script es un muy simple verificador de links que se asegura que las páginas que Nikola genera no tengan links rotos. Va a ser parte de Nikola propiamente dicho una vez que esté más pulido y doit soporte listar los targets
Para probarlo, bajalo y ejecutálo desde el mismo lugar
donde está tu conf.py, inmediatamente después de un doit
.
import os
import urllib
from urlparse import urlparse
import lxml.html
def analyze(filename):
try:
# Use LXML to parse the HTML
d = lxml.html.fromstring(open(filename).read())
for l in d.iterlinks():
# Get the target link
target = l[0].attrib[l[1]]
if target == "#": # These are always valid
continue
parsed = urlparse(target)
# We only handle relative links.
# TODO: check if the URL points to inside the generated
# site and check it anyway
if parsed.scheme:
continue
# Ignore the fragment, since the link will still work
# TODO: check that the fragment is valid
if parsed.fragment:
target = target.split('#')[0]
# Calculate what file or folder this points to
target_filename = os.path.abspath(
os.path.join(os.path.dirname(filename), urllib.unquote(target)))
# Check if it exists, or report it
if not os.path.exists(target_filename):
print "In %s broken link: " % filename, target
except Exception as exc:
# Something bad happened, report
print "Error with:", filename, exc
# This is hackish: we use doit to get a list of all
# generated files. Minor modifications would let you check
# the non-generated files as well.
for task in os.popen('doit list --all', 'r').readlines():
task = task.strip()
if task.split(':')[0] in (
'render_tags',
'render_archive',
'render_galleries',
'render_indexes',
'render_pages',
'render_site') and '.html' in task:
# It looks like a generated HTML file
analyze(task.split(":")[-1])