Permalink
Browse files

python 3 support

  • Loading branch information...
fortable1999 committed Jan 18, 2014
1 parent 4cd3787 commit 68893b31852c68140e9e2e9d282a59895fa562bb
Showing with 26 additions and 16 deletions.
  1. +3 −0 README.md
  2. +23 −16 sparkup.py
View
@@ -4,6 +4,9 @@ Sparkup
**Sparkup lets you write HTML code faster.** Don't believe us?
[See it in action!](http://www.youtube.com/watch?v=Jw3jipcenKc)
+Fixed by Zhao:
+This is a fork of original version. This version support both python 2 and 3.
+
You can write HTML in a CSS-like syntax, and have Sparkup handle the expansion to full HTML
code. It is meant to help you write long HTML blocks in your text editor by letting you
type less characters than needed.
View
@@ -1,13 +1,20 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-version = "0.1.3"
+version = "0.1.4"
import getopt
import sys
import re
# =============================================================================
+def iteritems(obj):
+ """iteritems() in python2 and items() in python3"""
+ if sys.version[0] == '2':
+ return obj.iteritems()
+ else:
+ return obj.items()
+
class Dialect:
shortcuts = {}
synonyms = {}
@@ -678,7 +685,7 @@ def get_default_tag(self):
"""
output = '%s' % (self.name)
- for key, value in self.attributes.iteritems():
+ for key, value in iteritems(self.attributes):
output += ' %s="%s"' % (key, value)
return output
@@ -764,7 +771,7 @@ def _fill_attributes(self):
# Make sure <a>'s have a href, <img>'s have an src, etc.
required = self.parser.dialect.required
- for element, attribs in required.iteritems():
+ for element, attribs in iteritems(required):
if self.name == element:
for attrib in attribs:
if attrib not in self.attributes:
@@ -860,7 +867,7 @@ def _init_element(self):
if ':' in name:
shortcuts = self.parser.dialect.shortcuts
if name in shortcuts.keys():
- for key, value in shortcuts[name].iteritems():
+ for key, value in iteritems(shortcuts[name]):
setattr(self, key, value)
if 'html' in name:
return
@@ -972,20 +979,20 @@ def start(self, options=None, str=None, ret=None):
return self.parse(str=str, ret=ret)
def help(self):
- print "Usage: %s [OPTIONS]" % sys.argv[0]
- print "Expands input into HTML."
- print ""
+ print("Usage: %s [OPTIONS]" % sys.argv[0])
+ print("Expands input into HTML.")
+ print("")
for short, long, info in self.options.cmdline_keys:
if "Deprecated" in info: continue
if not short == '': short = '-%s,' % short
if not long == '': long = '--%s' % long.replace("=", "=XXX")
- print "%6s %-25s %s" % (short, long, info)
- print ""
- print "\n".join(self.help_content)
+ print("%6s %-25s %s" % (short, long, info))
+ print("")
+ print("\n".join(self.help_content))
def version(self):
- print "Uhm, yeah."
+ print("Uhm, yeah.")
def parse(self, str=None, ret=None):
self.parser = Parser(self.options)
@@ -1013,8 +1020,8 @@ def parse(self, str=None, ret=None):
except:
sys.stderr.write("Parse error. Check your input.\n")
- print sys.exc_info()[0]
- print sys.exc_info()[1]
+ print(sys.exc_info()[0])
+ print(sys.exc_info()[1])
def exit(self):
sys.exit()
@@ -1032,7 +1039,7 @@ def __init__(self, router, argv, options=None):
# `options` can be given as a dict of stuff to preload
if options:
- for k, v in options.iteritems():
+ for k, v in iteritems(options):
self.options[k] = v
return
@@ -1066,11 +1073,11 @@ def __init__(self, router, argv, options=None):
elif key[0:1] == '-':
for short, long, info in self.cmdline_keys:
if short == key[1:]:
- print long
+ print(long)
options[long] = True
# Done
- for k, v in options.iteritems():
+ for k, v in iteritems(options):
self.options[k] = v
def __getattr__(self, attr):

0 comments on commit 68893b3

Please sign in to comment.