Skip to content

Instantly share code, notes, and snippets.

@waylan
waylan / foo.py
Created Dec 12, 2014
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.
View foo.py
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 Aug 29, 2015
A better Meta-Data handler for lightweight markup languages.
View docdata.py
"""
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 Mar 28, 2015
Alternate testing framework for python-markdown without nose.
View mdtest.py
import unittest
import os
import markdown
import codecs
import difflib
try:
import tidylib
except ImportError:
tidylib = None
@waylan
waylan / htmlspellchecker.py
Created Apr 11, 2015
Check the spelling of an HTML document.
View htmlspellchecker.py
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 Aug 29, 2015
Priority Sorted Registry
@waylan
waylan / mdtokenize.py
Last active Aug 29, 2015
Uselessly Simple Markdown Inline Tokenizer
View mdtokenize.py
"""
backtick `*
escape \\.
stem ***
st **
em *
stem ___
st __
em _
linkstart ]
@waylan
waylan / htree.py
Last active Aug 29, 2015
HTMLTree -- An HTML Node Tree toolkit. Warning! This is unfinished.
View htree.py
# -*- coding: utf-8 -*-
#
# HTMLTree
#
# An HTML Node Tree toolkit.
#
# --------------------------------------------------------------------
#
# Copyright (c) 2015 by Waylan Limberg. All rights reserved.
#
@waylan
waylan / parser.py
Last active Aug 29, 2015
A crazy idea I had as a way to parser HTML within a Markdown document.
View parser.py
"""
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 Jul 15, 2015
BeautifulSoup Document without parser - Currently broken!
View soup.py
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
The shlex module does not support Unicode input. Which workaround is faster?
View shlex_with_unicode.py
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."'