Commits

Oben Sonne  committed 7cdeac3

Port to Py3

Encoding options have been discarded, assuming in input and output
files.

  • Participants
  • Parent commits 6bb89c8
  • Branches py3

Comments (0)

Files changed (12)

 ~$
 ^.project$
 ^.pydevproject$
+^py3env$
 #
 # =============================================================================
 
-from __future__ import with_statement
-
 import codecs
 import glob
 import imp
 from os.path import exists as opx
 import re
 import shutil
-import StringIO
+import io
 import sys
 import traceback
-import urlparse
+import urllib.parse
 
-from SimpleHTTPServer import SimpleHTTPRequestHandler
-from BaseHTTPServer import HTTPServer
+from http.server import SimpleHTTPRequestHandler
+from http.server import HTTPServer
 
 try:
     import markdown
           "http://www.freewisdom.org/projects/python-markdown/Installation")
     sys.exit(1)
 
+UTF8 = 'UTF8'
+
 # =============================================================================
 # init site
 # =============================================================================
 "page.html": """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset={{ htmlspecialchars(__encoding__) }}" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - {{ htmlspecialchars(page["title"]) }}</title>
     <meta name="description" content="{{ htmlspecialchars(page.get("description", "a poole site")) }}" />
     <meta name="keywords" content="{{ htmlspecialchars(page.get("keywords", "poole")) }}" />
 posts.sort(key=lambda p: p.get("date"), reverse=True) # sort post pages by date
 for p in posts:
     date = datetime.strptime(p.date, "%Y-%m-%d").strftime("%B %d, %Y")
-    print "  * **[%s](%s)** - %s" % (p.post, p.url, date) # markdown list item
+    print("  * **[%s](%s)** - %s" % (p.post, p.url, date)) # markdown list item
 %-->
 
 Have a look into `input/blog.md` to see how it works. Feel free to adjust it
 *Posted at
 <!--%
 from datetime import datetime
-print datetime.strptime(page["date"], "%Y-%m-%d").strftime("%B %d, %Y")
+print(datetime.strptime(page["date"], "%Y-%m-%d").strftime("%B %d, %Y"))
 %-->*
 
 There is a bank in my eel, your argument is invalid.
         if virtual:
             self.raw = virtual
         else:
-            with codecs.open(fname, 'r', self._opts.input_enc) as fp:
+            with codecs.open(fname, 'r', UTF8) as fp:
                 self.raw = fp.readlines()
 
         # split raw content into macro definitions and real content
     regx_escp = re.compile(r'\\((?:(?:&lt;|<)!--|{)(?:{|%))') # escaped code
     repl_escp = r'\1'
     regx_rurl = re.compile(r'(?<=(?:(?:\n| )src|href)=")([^#/&%].*?)(?=")')
-    repl_rurl = lambda m: urlparse.urljoin(opts.base_url, m.group(1))
+    repl_rurl = lambda m: urllib.parse.urljoin(opts.base_url, m.group(1))
 
     regx_eval = re.compile(r'(?<!\\)(?:(?:<!--|{){)(.*?)(?:}(?:-->|}))', re.S)
 
         except:
             abort_iex(page, "expression", expr, traceback.format_exc())
         else:
-            if not isinstance(repl, basestring): # e.g. numbers
-                repl = unicode(repl)
-            elif not isinstance(repl, unicode):
-                repl = repl.decode("utf-8")
-            return repl
+            return str(repl)
 
     regx_exec = re.compile(r'(?<!\\)(?:(?:<!--|{)%)(.*?)(?:%(?:-->|}))', re.S)
 
         stmt = ind_rex.sub('', stmt)
 
         # execute
-        sys.stdout = StringIO.StringIO()
+        sys.stdout = io.StringIO()
         try:
-            exec stmt in macros.copy()
+            exec(stmt, macros.copy())
         except:
             sys.stdout = sys.__stdout__
             abort_iex(page, "statements", stmt, traceback.format_exc())
         else:
             repl = sys.stdout.getvalue()[:-1] # remove last line break
             sys.stdout = sys.__stdout__
-            if not isinstance(repl, unicode):
-                repl = repl.decode(opts.input_enc)
             return repl
 
     # -------------------------------------------------------------------------
     fname = opj(opts.project, "macros.py")
     macros = imp.load_source("macros", fname).__dict__ if opx(fname) else {}
 
-    macros["__encoding__"] = opts.output_enc
     macros["options"] = opts
     macros["project"] = project
     macros["input"] = dir_in
     pages = []
     custom_converter = macros.get('converter', {})
 
-    for cwd, dirs, files in os.walk(dir_in.decode(opts.filename_enc)):
+    for cwd, dirs, files in os.walk(dir_in):
         cwd_site = cwd[len(dir_in):].lstrip(os.path.sep)
         for sdir in dirs[:]:
             if re.search(opts.ignore, opj(cwd_site, sdir)):
     # render complete HTML pages
     # -------------------------------------------------------------------------
 
-    with codecs.open(opj(project, "page.html"), 'r', opts.input_enc) as fp:
+    with codecs.open(opj(project, "page.html"), 'r', UTF8) as fp:
         skeleton = fp.read()
 
     for page in pages:
         # write HTML page
         fname = page.fname.replace(dir_in, dir_out)
         fname = re.sub(MKD_PATT, ".html", fname)
-        with codecs.open(fname, 'w', opts.output_enc) as fp:
+        with codecs.open(fname, 'w', UTF8) as fp:
             fp.write(out)
 
     print("success: built project")
                   help="input files to ignore (default: '^\.|~$')")
     og.add_option("" , "--md-ext", default=[], metavar="EXT",
                   action="append", help="enable a markdown extension")
-    og.add_option("", "--input-enc", default="utf-8", metavar="ENC",
-                  help="encoding of input pages (default: utf-8)")
-    og.add_option("", "--output-enc", default="utf-8", metavar="ENC",
-                  help="encoding of output pages (default: utf-8)")
-    og.add_option("", "--filename-enc", default="utf-8", metavar="ENC",
-                  help="encoding of file names (default: utf-8)")
     op.add_option_group(og)
 
     og = optparse.OptionGroup(op, "Serve options")

File tests/expected/input/blog.2010-02-22.Doctors_in_my_penguin.md

 *Posted at
 <!--%
 from datetime import datetime
-print datetime.strptime(page["date"], "%Y-%m-%d").strftime("%B %d, %Y")
+print(datetime.strptime(page["date"], "%Y-%m-%d").strftime("%B %d, %Y"))
 %-->*
 
 There is a bank in my eel, your argument is invalid.

File tests/expected/input/blog.md

 posts.sort(key=lambda p: p.get("date"), reverse=True) # sort post pages by date
 for p in posts:
     date = datetime.strptime(p.date, "%Y-%m-%d").strftime("%B %d, %Y")
-    print "  * **[%s](%s)** - %s" % (p.post, p.url, date) # markdown list item
+    print("  * **[%s](%s)** - %s" % (p.post, p.url, date)) # markdown list item
 %-->
 
 Have a look into `input/blog.md` to see how it works. Feel free to adjust it

File tests/expected/output/blog.2010-02-22.Doctors_in_my_penguin.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - blog</title>
     <meta name="description" content="a poole site" />
     <meta name="keywords" content="poole" />

File tests/expected/output/blog.2010-03-01.I_ate_all the pokemans.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - blog</title>
     <meta name="description" content="a poole site" />
     <meta name="keywords" content="poole" />

File tests/expected/output/blog.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - blog</title>
     <meta name="description" content="a poole site" />
     <meta name="keywords" content="poole" />
 Poole recognizes the date and post title and sets them as attributes of the
 page. These attributes can then be used to generate a list of blog posts:</p>
 <ul>
-<li><strong><a href="/blog.2010-03-01.I_ate_all the pokemans.html">I ate all the pokemans</a></strong> - March 01, 2010</li>
+<li><strong><a href="/blog.2010-03-01.I_ate_all%20the%20pokemans.html">I ate all the pokemans</a></strong> - March 01, 2010</li>
 <li><strong><a href="/blog.2010-02-22.Doctors_in_my_penguin.html">Doctors in my penguin</a></strong> - February 22, 2010</li>
 </ul>
 <p>Have a look into <code>input/blog.md</code> to see how it works. Feel free to adjust it

File tests/expected/output/index.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - home</title>
     <meta name="description" content="a poole site" />
     <meta name="keywords" content="poole" />

File tests/expected/output/layout.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - layout</title>
     <meta name="description" content="a poole site" />
     <meta name="keywords" content="poole" />

File tests/expected/output/logic.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - logic</title>
     <meta name="description" content="a poole site" />
     <meta name="keywords" content="poole" />

File tests/expected/page.html

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
 <head>
-    <meta http-equiv="Content-Type" content="text/html; charset={{ htmlspecialchars(__encoding__) }}" />
+    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
     <title>poole - {{ htmlspecialchars(page["title"]) }}</title>
     <meta name="description" content="{{ htmlspecialchars(page.get("description", "a poole site")) }}" />
     <meta name="keywords" content="{{ htmlspecialchars(page.get("keywords", "poole")) }}" />

File tests/run.py

 #!/usr/bin/env python
 
-import codecs
 import os
 import shutil
 import subprocess
 p = subprocess.Popen(cmd_diff, stdout=subprocess.PIPE)
 diff = p.communicate()[0]
 if diff:
-    with codecs.open(ERRORS, 'w', 'UTF8') as fp:
+    with open(ERRORS, 'wb') as fp:
         fp.write(diff)
     print("failed - see %s for details" % ERRORS)
     sys.exit(1)