Skip to content

Instantly share code, notes, and snippets.

@waylan
waylan / foo.py
Created December 12, 2014 14:14
Class in which only one instance of each subclass can exist in a set. Not sure if there is a better way to do this, but it works. Although I don't like that overriding `__eg__` breaks other things.
class Foo(object):
def __hash__(self):
return hash('.'.join([self.__class__.__module__, self.__class__.__name__]))
def __eq__(a, b):
return a.__hash__() == b.__hash__()
class Bar(Foo):
@waylan
waylan / docdata.py
Last active August 29, 2015 14:16
A better Meta-Data handler for lightweight markup languages.
"""
DocData
A Meta-Data handler for lightweight markup languages.
Note: This was an experiment which was rejected in favor of a different API.
See the better implementation here: <https://github.com/waylan/docdata>
An implementation of Meta-Data as defined by MultiMarkdown. However, it can
work with any lightweight markup language and the various keys can have
@waylan
waylan / mdtest.py
Created March 28, 2015 03:13
Alternate testing framework for python-markdown without nose.
import unittest
import os
import markdown
import codecs
import difflib
try:
import tidylib
except ImportError:
tidylib = None
@waylan
waylan / htmlspellchecker.py
Created April 11, 2015 00:50
Check the spelling of an HTML document.
try:
from html.parser import HTMLParser
except ImportError:
from HTMLParser import HTMLParser
from enchant.checker import SpellChecker
class HTMLSpellChecker(HTMLParser):
"""
Check the spelling of an HTML document using the given SpellChecker.
@waylan
waylan / .gitignore
Last active August 29, 2015 14:23
Priority Sorted Registry
*.pyc
@waylan
waylan / mdtokenize.py
Last active August 29, 2015 14:23
Uselessly Simple Markdown Inline Tokenizer
"""
backtick `*
escape \\.
stem ***
st **
em *
stem ___
st __
em _
linkstart ]
@waylan
waylan / htree.py
Last active August 29, 2015 14:24
HTMLTree -- An HTML Node Tree toolkit. Warning! This is unfinished.
# -*- coding: utf-8 -*-
#
# HTMLTree
#
# An HTML Node Tree toolkit.
#
# --------------------------------------------------------------------
#
# Copyright (c) 2015 by Waylan Limberg. All rights reserved.
#
@waylan
waylan / parser.py
Last active August 29, 2015 14:24
A crazy idea I had as a way to parser HTML within a Markdown document.
"""
A crazy Idea!
Just maybe, the way to parse HTML within a Markdown document is to run the document
through an HTML Parser first. Some parsers, like the HTMLParser included in the
Python Standard lib will properly parse the plain text not wrapped in HTML tags
as plain text and simply return it unaltered. The problem is with Markdown's
autolinks (`<foo@bar.com>` and `<http://example.com>`).
However, as of Python 2.7.3 and 3.2.2, the HTMLParser can now handle invalid HTML
@waylan
waylan / soup.py
Created July 15, 2015 00:38
BeautifulSoup Document without parser - Currently broken!
from bs4.element import Tag, NavigableString
class Doc(Tag):
"""
Dumby Document Root.
This class provides a document root object without a parser.
The document will need to be built mannually by adding Tags,
NavigableStrings, Comments and the like. This class assumes
@waylan
waylan / shlex_with_unicode.py
Created May 12, 2011 18:42
The shlex module does not support Unicode input. Which workaround is faster?
import shlex
import re
# Testing shlex with Unicode.
#
# The shlex module does not support Unicode input. Which workaround is faster?
# Sample string. Using an Attribute List from Maruku's syntax:
# http://maruku.rubyforge.org/proposal.html#attribute_lists
t = u'.foo #bar class=foo ref title="Foo \xc3 bar."'