alecwh /

Full commit
web log data handler
processes and handles article data/content modifications (quick filter items, such as [code][/code])
$author: alecwh
$date: 08/19/2009 

from config import config
import log
import time
import re

# for comment, article, and page formatting
import markdown

def parse_date(timestring):
	Parses a unix timestamp into an object
	which we can use to grab date info
	date_object = time.gmtime(float(timestring))
	return date_object

def human_timestamp(timestamp, element="article"):
	Converts a unix-style timestamp to a readable format
	based on display.date_format in config.rc
	# create object, format it with config.display.date_format
	date_obj = parse_date(timestamp)
	if element == "article":
		format_to_use = config.display.date_format
	elif element == "comment":
		format_to_use = config.comments.date_format
	formatted = time.strftime(format_to_use, date_obj)
	return unicode(formatted)

def rfc_timestamp(timestamp):
	Converts timestamp to RFC 2822 compatible format
	return strftime("%a, %d %b %Y %H:%M:%S +0000", timestamp)
def hyperlink_tags(tags):
	Takes a list of tags (tags,like,this)
	and turns each tag into a hyperlink
	tags = tags.split(",")
	formatted = []
	for tag in tags:
		tag = '<a class="tag" href="%s+tags/%s">%s</a>' % (config.general.base_url, tag.strip(), tag.strip())
	return ', '.join(formatted)
def Markdown2HTML(text, tabs=0):
	Converts Markdown text to HTML
	Markdown = markdown.Markdown()
	text = Markdown.convert(text)
	# indent properly
	tabs = "\t"*tabs
	text = text.replace("\n", "\n%s" % tabs)
	return text

def apply_filters(text):
	Replaces all occurences of orig in text with new
	# filters are defined in config.rc
	# under the category "filters"
	# just enumerate them: 'filter1, filter2...'
	# seperated by |
	for x, y in config.filters:
		pair = getattr(config.filters, x).split("|")
		text = text.replace(pair[0], pair[1])
	return text
# The following functions handle processing of all text in
# articles and comments.
# They apply many of the functions in this module
def handle_article_content(article):
	Takes an article's content (text, title, ...) and applies
	all special tasks to the text. Filters, Markdown,
	plugins, etc.
	**This is only executed with data being presented, not stored!**
	article.content = Markdown2HTML(apply_filters(article.content), 0)
	article.timestamp = human_timestamp(article.timestamp)
	article.tags = hyperlink_tags(article.tags)
	return article
def handle_comment_content(comment):
	Takes a comment's content (author, text, email) and applies
	all special tasks to the text.
	**This is only executed with data being presented, not stored!**
	#TODO: Add pygments parsing here
	comment.text = Markdown2HTML(comment.text)
	comment.timestamp = human_timestamp(comment.timestamp, "comment").lower()
	return comment
if __name__ == "__main__":
	text = """
[code] $ sudo xkill kyle[/code]
	print apply_filters(text)