Skip to content

Commit

Permalink
Overhaul. GitHub style and Pygments syntax!
Browse files Browse the repository at this point in the history
  • Loading branch information
cluther committed Aug 10, 2012
1 parent 91186fd commit 98288a8
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 102 deletions.
11 changes: 2 additions & 9 deletions Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleVersion</key>
<string>1.2.1</string>
<string>2.0.0</string>
<key>CFBundleShortVersionString</key>
<string>1.2.1</string>
<string>2.0.0</string>
<key>CFPlugInDynamicRegisterFunction</key>
<string></string>
<key>CFPlugInDynamicRegistration</key>
Expand All @@ -73,19 +73,12 @@
</dict>
<key>CFPlugInUnloadFunction</key>
<string></string>
<!-- Change following property to <true/> if the generators supports
multiple concurrent requests -->
<key>QLSupportsConcurrentRequests</key>
<false/>
<!-- Change following property to <true/> if the generators needs
to be run on main thread -->
<key>QLNeedsToBeRunInMainThread</key>
<true/>
<!-- Change following property to indicate the minimum useful size for a thumbnail the generator
can produce. If your generator is fast enough, you can remove the minimum size so to appear in lists -->
<key>QLThumbnailMinimumSize</key>
<real>17</real>
<!-- Change following properites to indicate the preview size to use if preview generation takes too long -->
<key>QLPreviewWidth</key>
<real>800</real>
<key>QLPreviewHeight</key>
Expand Down
39 changes: 6 additions & 33 deletions QLRest.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,10 @@
0A80BFCC0E21B61A00C8BF14 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0A80BFCB0E21B61A00C8BF14 /* WebKit.framework */; };
2C05A19C06CAA52B00D84F6F /* GeneratePreviewForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 2C05A19B06CAA52B00D84F6F /* GeneratePreviewForURL.m */; };
61E3BCFB0870B4F2002186A0 /* GenerateThumbnailForURL.m in Sources */ = {isa = PBXBuildFile; fileRef = 61E3BCFA0870B4F2002186A0 /* GenerateThumbnailForURL.m */; };
867503B715D561C70097AED5 /* styles.css in Resources */ = {isa = PBXBuildFile; fileRef = 867503B615D561C70097AED5 /* styles.css */; };
8D576312048677EA00EA77CD /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 08FB77B6FE84183AC02AAC07 /* main.c */; settings = {ATTRIBUTES = (); }; };
8D576314048677EA00EA77CD /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0AA1909FFE8422F4C02AAC07 /* CoreFoundation.framework */; };
8D5B49A804867FD3000E48DA /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8D5B49A704867FD3000E48DA /* InfoPlist.strings */; };
AB40554314D008B3009A583A /* blocktags in Resources */ = {isa = PBXBuildFile; fileRef = AB40553E14D008B3009A583A /* blocktags */; };
AB40554414D008B3009A583A /* config.h in Headers */ = {isa = PBXBuildFile; fileRef = AB40553F14D008B3009A583A /* config.h */; };
AB40554514D008B3009A583A /* mkdio.h in Headers */ = {isa = PBXBuildFile; fileRef = AB40554014D008B3009A583A /* mkdio.h */; };
AB40554714D008B3009A583A /* update.sh in Resources */ = {isa = PBXBuildFile; fileRef = AB40554214D008B3009A583A /* update.sh */; };
AB40554A14D00A6A009A583A /* update-discount.sh in Resources */ = {isa = PBXBuildFile; fileRef = AB40554914D00A6A009A583A /* update-discount.sh */; };
AB40555514D00CCC009A583A /* Csio.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555014D00CCC009A583A /* Csio.c */; };
AB40555614D00CCC009A583A /* emmatch.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555114D00CCC009A583A /* emmatch.c */; };
AB40555714D00CCC009A583A /* generate.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555214D00CCC009A583A /* generate.c */; };
AB40555814D00CCC009A583A /* html5.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555314D00CCC009A583A /* html5.c */; };
AB40556414D00CFD009A583A /* mkdio.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555C14D00CFD009A583A /* mkdio.c */; };
AB40556514D00CFD009A583A /* resource.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555D14D00CFD009A583A /* resource.c */; };
AB40556614D00CFD009A583A /* setup.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555E14D00CFD009A583A /* setup.c */; };
AB40556714D00CFD009A583A /* tags.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40555F14D00CFD009A583A /* tags.c */; };
AB40556814D00CFD009A583A /* tags.h in Headers */ = {isa = PBXBuildFile; fileRef = AB40556014D00CFD009A583A /* tags.h */; };
AB40556914D00CFD009A583A /* xml.c in Sources */ = {isa = PBXBuildFile; fileRef = AB40556114D00CFD009A583A /* xml.c */; };
C86B05270671AA6E00DD9006 /* CoreServices.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C86B05260671AA6E00DD9006 /* CoreServices.framework */; };
EE245D150F530DF600A4E708 /* Python.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = EE245D140F530DF600A4E708 /* Python.framework */; };
EEC528820F52A16100F78FC5 /* RestConverter.h in Headers */ = {isa = PBXBuildFile; fileRef = EEC528800F52A16100F78FC5 /* RestConverter.h */; };
Expand All @@ -45,22 +31,9 @@
0AA1909FFE8422F4C02AAC07 /* CoreFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreFoundation.framework; path = /System/Library/Frameworks/CoreFoundation.framework; sourceTree = "<absolute>"; };
2C05A19B06CAA52B00D84F6F /* GeneratePreviewForURL.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratePreviewForURL.m; sourceTree = "<group>"; };
61E3BCFA0870B4F2002186A0 /* GenerateThumbnailForURL.m */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.objc; path = GenerateThumbnailForURL.m; sourceTree = "<group>"; };
867503B615D561C70097AED5 /* styles.css */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.css; path = styles.css; sourceTree = "<group>"; };
8D576316048677EA00EA77CD /* QLRest.qlgenerator */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = QLRest.qlgenerator; sourceTree = BUILT_PRODUCTS_DIR; };
8D576317048677EA00EA77CD /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
AB40553E14D008B3009A583A /* blocktags */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = blocktags; sourceTree = "<group>"; };
AB40553F14D008B3009A583A /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = "<group>"; };
AB40554014D008B3009A583A /* mkdio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = mkdio.h; sourceTree = "<group>"; };
AB40554214D008B3009A583A /* update.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = update.sh; sourceTree = "<group>"; };
AB40554914D00A6A009A583A /* update-discount.sh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.script.sh; path = "update-discount.sh"; sourceTree = "<group>"; };
AB40555014D00CCC009A583A /* Csio.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = Csio.c; sourceTree = "<group>"; };
AB40555114D00CCC009A583A /* emmatch.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = emmatch.c; sourceTree = "<group>"; };
AB40555214D00CCC009A583A /* generate.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = generate.c; sourceTree = "<group>"; };
AB40555314D00CCC009A583A /* html5.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = html5.c; sourceTree = "<group>"; };
AB40555D14D00CFD009A583A /* resource.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = resource.c; sourceTree = "<group>"; };
AB40555E14D00CFD009A583A /* setup.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = setup.c; sourceTree = "<group>"; };
AB40555F14D00CFD009A583A /* tags.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = tags.c; sourceTree = "<group>"; };
AB40556014D00CFD009A583A /* tags.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = tags.h; sourceTree = "<group>"; };
AB40556114D00CFD009A583A /* xml.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = xml.c; sourceTree = "<group>"; };
C86B05260671AA6E00DD9006 /* CoreServices.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreServices.framework; path = /System/Library/Frameworks/CoreServices.framework; sourceTree = "<absolute>"; };
EE245D140F530DF600A4E708 /* Python.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Python.framework; path = /System/Library/Frameworks/Python.framework; sourceTree = "<absolute>"; };
EEC528800F52A16100F78FC5 /* RestConverter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RestConverter.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -115,6 +88,7 @@
089C167CFE841241C02AAC07 /* Resources */ = {
isa = PBXGroup;
children = (
867503B615D561C70097AED5 /* styles.css */,
8D576317048677EA00EA77CD /* Info.plist */,
8D5B49A704867FD3000E48DA /* InfoPlist.strings */,
);
Expand All @@ -124,8 +98,6 @@
08FB77AFFE84173DC02AAC07 /* Source */ = {
isa = PBXGroup;
children = (
4446CFF20F41899C00BAC3F4 /* discount-wrapper.c */,
4446CFF30F41899C00BAC3F4 /* discount-wrapper.h */,
61E3BCFA0870B4F2002186A0 /* GenerateThumbnailForURL.m */,
2C05A19B06CAA52B00D84F6F /* GeneratePreviewForURL.m */,
08FB77B6FE84183AC02AAC07 /* main.c */,
Expand Down Expand Up @@ -207,6 +179,7 @@
buildActionMask = 2147483647;
files = (
8D5B49A804867FD3000E48DA /* InfoPlist.strings in Resources */,
867503B715D561C70097AED5 /* styles.css in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -290,8 +263,8 @@
buildSettings = {
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
PRODUCT_NAME = QLRest;
MACOSX_DEPLOYMENT_TARGET = 10.6;
PRODUCT_NAME = QLRest;
SDKROOT = "";
WARNING_CFLAGS = "-Wall";
};
Expand All @@ -304,8 +277,8 @@
ARCHS_STANDARD_32_64_BIT_PRE_XCODE_3_1 = "ppc i386 ppc64 x86_64";
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
PRODUCT_NAME = QLRest;
MACOSX_DEPLOYMENT_TARGET = 10.6;
PRODUCT_NAME = QLRest;
SDKROOT = "";
WARNING_CFLAGS = "-Wall";
};
Expand Down
164 changes: 104 additions & 60 deletions RestConverter.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,108 @@

NSData* renderRest(NSString* source) {
// Renders the contents of a NSURL from rest source into HTML.
// Requires that Python and docutils be installed.
// Returns NULL for any kind of failure.

PyTupleObject *pmethodargs;
PyDictObject *pmethodnamedargs;
PyObject *psource, *pwriter, *pmodule, *pmethod, *presult;

char* output;

// Requires that Python, docutils and pygments be installed.

NSString *html = NULL;

Py_Initialize();

// TODO: there's no exception handling going on here at all.
// TODO: Is it possible to make the warnings go away?

pmethodargs = PyTuple_New(1);
if (pmethodargs == NULL) return NULL;
pmethodnamedargs = PyDict_New();
if (pmethodnamedargs == NULL) return NULL;

psource = PyString_FromString([source UTF8String]);
if (psource == NULL) return NULL;
PyTuple_SetItem(pmethodargs, 0, psource);

pwriter = PyString_FromString("html");
if (pwriter == NULL) return NULL;
PyDict_SetItemString(pmethodnamedargs, "writer_name", pwriter);
pmodule = PyImport_ImportModule("docutils.core");
//if (PyErr_Occurred())
// PyErr_Print();
if (pmodule == NULL) return NULL;

pmethod = PyObject_GetAttrString(pmodule, "publish_string");
//if (PyErr_Occurred())
// PyErr_Print();
if (pmethod == NULL) return NULL;

presult = PyObject_Call(pmethod, pmethodargs, pmethodnamedargs);
//if (PyErr_Occurred())
// PyErr_Print();
if (presult == NULL) return NULL;

PyArg_Parse(presult, "s", &output);
//if (PyErr_Occurred())
// PyErr_Print();


Py_DECREF(presult);
Py_DECREF(pmethod);
Py_DECREF(pmodule);
Py_DECREF(pwriter);
Py_DECREF(psource);
Py_DECREF(pmethodnamedargs);
Py_DECREF(pmethodargs);

NSString *html = [NSString stringWithUTF8String:output];

// TODO: Why does this cause segfault?
//Py_Finalize();

return [html dataUsingEncoding:NSUTF8StringEncoding];
}

PyObject *pSettingsDict;
pSettingsDict = PyDict_New();
PyDict_SetItemString(pSettingsDict, "file_insertion_enabled", Py_False);
PyDict_SetItemString(pSettingsDict, "raw_enabled", Py_False);
PyDict_SetItemString(pSettingsDict, "strip_comments", Py_True);
PyDict_SetItemString(pSettingsDict, "doctitle_xform", Py_False);
PyDict_SetItemString(pSettingsDict, "report_level", PyInt_FromLong(5));
PyDict_SetItemString(pSettingsDict, "syntax_highlight", PyString_FromString("short"));
PyDict_SetItemString(pSettingsDict, "math_output", PyString_FromString("latex"));

PyObject *pDocutilsCoreModule;
pDocutilsCoreModule = PyImport_ImportModule("docutils.core");

PyObject *pPublishMethod;
pPublishMethod = PyObject_GetAttrString(pDocutilsCoreModule, "publish_parts");

PyObject *pPublishArgs;
pPublishArgs = PyTuple_New(1);
PyTuple_SetItem(pPublishArgs, 0, PyString_FromString([source UTF8String]));

PyObject *pPublishKwargs;
pPublishKwargs = PyDict_New();
PyDict_SetItemString(pPublishKwargs, "writer_name", PyString_FromString("html"));
PyDict_SetItemString(pPublishKwargs, "settings_overrides", pSettingsDict);

PyObject *pParts;
pParts = PyObject_Call(pPublishMethod, pPublishArgs, pPublishKwargs);

Py_DECREF(pSettingsDict);
Py_DECREF(pDocutilsCoreModule);
Py_DECREF(pPublishMethod);
Py_DECREF(pPublishArgs);
Py_DECREF(pPublishKwargs);

if (pParts == NULL && PyErr_Occurred()) {
PyObject *pErrType, *pErrValue, *pTraceback;
PyErr_Fetch(&pErrType, &pErrValue, &pTraceback);
if (pErrValue != NULL)
html = [NSString stringWithFormat:@
"<html>"
"<body>"
"<h2>Error Parsing reStructuredText</h2>"
"<hr/>"
"<pre>"
"%@"
"</pre>"
"<h2>Source</h2>"
"<hr/>"
"<pre>"
"%@"
"</pre>"
"</body>"
"</html>",
[NSString stringWithUTF8String:PyString_AsString(PyObject_Str(pErrValue))],
source];

Py_XDECREF(pErrType);
Py_XDECREF(pErrValue);
Py_XDECREF(pTraceback);

if (html != NULL)
return [html dataUsingEncoding:NSUTF8StringEncoding];
}

PyObject *pBody;
pBody = PyDict_GetItemString(pParts, "html_body");

if (pBody != NULL) {
NSString *styles = [NSString stringWithContentsOfFile:[[NSBundle bundleWithIdentifier: @"net.pixane.qlgenerator.QLRest"]
pathForResource:@"styles" ofType:@"css"]
encoding:NSUTF8StringEncoding
error:nil];

html = [NSString stringWithFormat:@
"<html>"
"<head>"
"<meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\" />"
"<style type=\"text/css\">"
"%@"
"article { background-color:white; padding:20px; }"
"</style>"
"</head>"
"<body>"
"<div class=\"announce instapaper_body rst\" data-path=\"/\" id=\"readme\">"
"<article class=\"markdown-body entry-content\" itemprop=\"mainContentOfPage\">"
"%@"
"</article>"
"</div>"
"</body>"
"</html>",
styles, [NSString stringWithUTF8String:PyString_AsString(pBody)]];

Py_DECREF(pBody);

return [html dataUsingEncoding:NSUTF8StringEncoding];
}

return NULL;
}
63 changes: 63 additions & 0 deletions styles.css

Large diffs are not rendered by default.

0 comments on commit 98288a8

Please sign in to comment.