redirect/gone cleanup, step 3; moved syndication links to the top of an entry
This commit is contained in:
parent
b46d8aa7a5
commit
0f298ae2ac
1 changed files with 33 additions and 27 deletions
60
nasg.py
60
nasg.py
|
@ -48,22 +48,24 @@ logger = logging.getLogger("NASG")
|
||||||
|
|
||||||
MarkdownImage = namedtuple("MarkdownImage", ["match", "alt", "fname", "title", "css"])
|
MarkdownImage = namedtuple("MarkdownImage", ["match", "alt", "fname", "title", "css"])
|
||||||
|
|
||||||
J2 = jinja2.Environment(
|
|
||||||
loader=jinja2.FileSystemLoader(searchpath=settings.paths.get("tmpl")),
|
|
||||||
lstrip_blocks=True,
|
|
||||||
trim_blocks=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
RE_MDIMG = re.compile(
|
RE_MDIMG = re.compile(
|
||||||
r"(?P<match>!\[(?P<alt>[^\]]+)?\]\((?P<fname>[^\s\]]+)"
|
r"(?P<match>!\[(?P<alt>[^\]]+)?\]\((?P<fname>[^\s\]]+)"
|
||||||
r"(?:\s[\'\"](?P<title>[^\"\']+)[\'\"])?\)(?:{(?P<css>[^\}]+)\})?)",
|
r"(?:\s[\'\"](?P<title>[^\"\']+)[\'\"])?\)(?:{(?P<css>[^\}]+)\})?)",
|
||||||
re.IGNORECASE,
|
re.IGNORECASE,
|
||||||
)
|
)
|
||||||
|
|
||||||
RE_CODE = re.compile(r"^(?:[~`]{3,4}).+$", re.MULTILINE)
|
RE_CODE = re.compile(
|
||||||
|
r'^(?:[~`]{3,4}).+$',
|
||||||
|
re.MULTILINE
|
||||||
|
)
|
||||||
|
|
||||||
RE_PRECODE = re.compile(r'<pre class="([^"]+)"><code>')
|
RE_PRECODE = re.compile(
|
||||||
|
r'<pre class="([^"]+)"><code>'
|
||||||
|
)
|
||||||
|
|
||||||
|
RE_MYURL = re.compile(
|
||||||
|
r'(^(%s[^"]+)$|"(%s[^"]+)")' % (settings.site.url, settings.site.url)
|
||||||
|
)
|
||||||
|
|
||||||
def mtime(path):
|
def mtime(path):
|
||||||
""" return seconds level mtime or 0 (chomp microsecs) """
|
""" return seconds level mtime or 0 (chomp microsecs) """
|
||||||
|
@ -83,32 +85,16 @@ def url2slug(url, limit=200):
|
||||||
:limit
|
:limit
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
J2.filters["url2slug"] = url2slug
|
|
||||||
|
|
||||||
|
|
||||||
def rfc3339todt(rfc3339):
|
def rfc3339todt(rfc3339):
|
||||||
""" nice dates for humans """
|
""" nice dates for humans """
|
||||||
t = arrow.get(rfc3339).format("YYYY-MM-DD HH:mm ZZZ")
|
t = arrow.get(rfc3339).format("YYYY-MM-DD HH:mm ZZZ")
|
||||||
return "%s" % (t)
|
return "%s" % (t)
|
||||||
|
|
||||||
|
|
||||||
J2.filters["printdate"] = rfc3339todt
|
|
||||||
|
|
||||||
|
|
||||||
def extractlicense(url):
|
def extractlicense(url):
|
||||||
""" extract license name """
|
""" extract license name """
|
||||||
n, e = os.path.splitext(os.path.basename(url))
|
n, e = os.path.splitext(os.path.basename(url))
|
||||||
return n.upper()
|
return n.upper()
|
||||||
|
|
||||||
|
|
||||||
J2.filters["extractlicense"] = extractlicense
|
|
||||||
|
|
||||||
RE_MYURL = re.compile(
|
|
||||||
r'(^(%s[^"]+)$|"(%s[^"]+)")' % (settings.site.url, settings.site.url)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def relurl(text, baseurl=None):
|
def relurl(text, baseurl=None):
|
||||||
if not baseurl:
|
if not baseurl:
|
||||||
baseurl = settings.site.url
|
baseurl = settings.site.url
|
||||||
|
@ -130,9 +116,6 @@ def relurl(text, baseurl=None):
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
|
||||||
J2.filters["relurl"] = relurl
|
|
||||||
|
|
||||||
|
|
||||||
def writepath(fpath, content, mtime=0):
|
def writepath(fpath, content, mtime=0):
|
||||||
""" f.write with extras """
|
""" f.write with extras """
|
||||||
d = os.path.dirname(fpath)
|
d = os.path.dirname(fpath)
|
||||||
|
@ -147,6 +130,27 @@ def writepath(fpath, content, mtime=0):
|
||||||
logger.info("writing file %s", fpath)
|
logger.info("writing file %s", fpath)
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
|
#def maybe_copy(source, target):
|
||||||
|
#""" copy only if target mtime is smaller, than source mtime """
|
||||||
|
#if os.path.exists(target) and mtime(source) <= mtime(target):
|
||||||
|
#return
|
||||||
|
#logger.info("copying '%s' to '%s'", source, target)
|
||||||
|
#cp(source, target)
|
||||||
|
|
||||||
|
def extractdomain(url):
|
||||||
|
url = urlparse(url)
|
||||||
|
return url.netloc
|
||||||
|
|
||||||
|
J2 = jinja2.Environment(
|
||||||
|
loader=jinja2.FileSystemLoader(searchpath=settings.paths.get("tmpl")),
|
||||||
|
lstrip_blocks=True,
|
||||||
|
trim_blocks=True,
|
||||||
|
)
|
||||||
|
J2.filters["relurl"] = relurl
|
||||||
|
J2.filters["url2slug"] = url2slug
|
||||||
|
J2.filters["printdate"] = rfc3339todt
|
||||||
|
J2.filters["extractlicense"] = extractlicense
|
||||||
|
J2.filters["extractdomain"] = extractdomain
|
||||||
|
|
||||||
class cached_property(object):
|
class cached_property(object):
|
||||||
""" extermely simple cached_property decorator:
|
""" extermely simple cached_property decorator:
|
||||||
|
@ -1640,6 +1644,8 @@ class IndexPHP(PHPFile):
|
||||||
"menu": settings.menu,
|
"menu": settings.menu,
|
||||||
"gones": self.gone,
|
"gones": self.gone,
|
||||||
"redirects": self.redirect,
|
"redirects": self.redirect,
|
||||||
|
"rewrites": settings.rewrites,
|
||||||
|
"gone_re": settings.gones
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
writepath(self.renderfile, r)
|
writepath(self.renderfile, r)
|
||||||
|
|
Loading…
Reference in a new issue