From e64c4924f4a052f5cc45e7d631681cffdb19b62a Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Sat, 21 Jun 2014 18:14:52 -0400 Subject: [PATCH] API documentation, fixes, support not highlighting (closes #56). --- app.py | 14 +-- bitshift/codelet.py | 17 +++- static/js/index.js | 17 +--- static/sass/developers.sass | 7 -- static/sass/docs.sass | 6 +- static/sitemap.xml | 4 +- templates/developers.html | 18 ---- templates/docs.html | 242 +++++++++++++++++++++++++++++++++++++------- templates/layout.html | 1 - test/find_function_def.py | 19 ++++ 10 files changed, 249 insertions(+), 96 deletions(-) delete mode 100644 static/sass/developers.sass delete mode 100644 templates/developers.html create mode 100755 test/find_function_def.py diff --git a/app.py b/app.py index 36add14..f05ba67 100644 --- a/app.py +++ b/app.py @@ -31,29 +31,31 @@ def search(): resp.mimetype = "application/json" return resp - query, page = request.args.get("q"), request.args.get("p", 1) + query = request.args.get("q") if not query: return reply({"error": "No query given"}) try: tree = parse_query(query) except QueryParseException as exc: return reply({"error": exc.args[0]}) + + page = request.args.get("p", 1) try: page = int(page) except ValueError: return reply({"error": u"Invalid page number: %s" % page}) + + highlight = request.args.get("hl", "0") + highlight = highlight.lower() not in ["0", "false", "no"] + count, codelets = database.search(tree, page) - results = [clt.serialize() for clt in codelets] + results = [clt.serialize(highlight) for clt in codelets] return reply({"count": count, "results": results}) @app.route("/about") def about(): return render_template("about.html") -@app.route("/developers") -def developers(): - return render_template("developers.html") - @app.route("/docs") def docs(): return render_template("docs.html") diff --git a/bitshift/codelet.py b/bitshift/codelet.py index 8bc3908..173727d 100644 --- a/bitshift/codelet.py +++ b/bitshift/codelet.py @@ -74,18 +74,25 @@ class Codelet(object): self.symbols = symbols or {} self.origin = origin or (None, None) - def serialize(self): + def serialize(self, highlight=False): """ Convert the codelet into a dictionary that can be sent as JSON. + :param highlight: Whether to return code as pygments-highlighted HTML + or as plain source. + :type highlight: bool + :return: The codelet as a dictionary. :rtype: str """ lang = LANGS[self.language] - lines = reduce(concat, [[loc[0] for loc in sym[1] + sym[2]] for sym in - reduce(concat, self.symbols.values(), [])], []) - formatter = HtmlFormatter(linenos=True, hl_lines=lines) - code = highlight(self.code, get_lexer_by_name(lang.lower()), formatter) + code = self.code + if highlight: + symbols = reduce(concat, self.symbols.values(), []) + lines = reduce(concat, [[loc[0] for loc in sym[1] + sym[2]] + for sym in symbols], []) + formatter = HtmlFormatter(linenos=True, hl_lines=lines) + code = highlight(code, get_lexer_by_name(lang.lower()), formatter) return { "name": self.name, "code": code, "lang": lang, diff --git a/static/js/index.js b/static/js/index.js index 0b9b550..b50b915 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -136,20 +136,6 @@ var searchResultsPage = 1; }); }()); -//Obtained by parsing python file with pygments -var codeExample = '
 1\n 2\n 3\n 4\n 5\n 6\n 7\n 8\n 9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n30\n31\n32\n33\n34\n35\n36\n37\n38\n39\n40
"""\nModule to contain all the project's Flask server plumbing.\n"""\n\nfrom flask import Flask\nfrom flask import render_template, session\n\nfrom bitshift import assets\n# from bitshift.database import Database\n# from bitshift.query import parse_query\n\napp = Flask(__name__)\napp.config.from_object("bitshift.config")\n\napp_env = app.jinja_env\napp_env.line_statement_prefix = "="\napp_env.globals.update(assets=assets)\n\n# database = Database()\n\n@app.route("/")\ndef index():\n    return render_template("index.html")\n\n@app.route("/search/<query>")\ndef search(query):\n    # tree = parse_query(query)\n    # database.search(tree)\n    pass\n\n@app.route("/about")\ndef about():\n    return render_template("about.html")\n\n@app.route("/developers")\ndef developers():\n    return render_template("developers.html")\n\nif __name__ == "__main__":\n    app.run(deaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabug=True)\n
\n
' - -var testCodelet = { - 'url': 'https://github.com/earwig/bitshift/blob/develop/app.py', - 'name': 'app.py', - 'lang': 'python', - 'created': 'May 10, 2014 aaaaaaaaaa', - 'modified': '2 days ago', - 'origin': ['GitHub', 'https://github.com', ''], - 'authors': ['sevko', 'earwig', 'another author', 'a'], - 'code': codeExample -}; - // Enable infinite scrolling down the results page. $(window).scroll(function() { var searchField = $("div#search-field"); @@ -396,7 +382,8 @@ function nextSymbolMatch() { function queryServer(){ var queryUrl = document.URL + "search.json?" + $.param({ "q" : searchBar.value, - "p" : searchResultsPage++ + "p" : searchResultsPage++, + "hl": 1 }); var results = $.Deferred(); diff --git a/static/sass/developers.sass b/static/sass/developers.sass deleted file mode 100644 index 419dc3e..0000000 --- a/static/sass/developers.sass +++ /dev/null @@ -1,7 +0,0 @@ -@import variables - -a - text-decoration: none - -h1 - color: $baseColor1 diff --git a/static/sass/docs.sass b/static/sass/docs.sass index 6d23e32..4d851df 100644 --- a/static/sass/docs.sass +++ b/static/sass/docs.sass @@ -21,17 +21,17 @@ ul margin-bottom: 2% span - &#code + &.code background-color: $baseColor3 * 1.2 font-family: monospace padding: 5px - &#string + &.string color: $baseColor1 font-family: monospace font-size: 1.1em - &#title + &.title color: $baseColor1 font-weight: bold diff --git a/static/sitemap.xml b/static/sitemap.xml index ff8ccbf..24adafe 100644 --- a/static/sitemap.xml +++ b/static/sitemap.xml @@ -4,13 +4,13 @@ http://bitshift.it/ monthly - + http://bitshift.it/about monthly - http://bitshift.it/developers + http://bitshift.it/docs monthly diff --git a/templates/developers.html b/templates/developers.html deleted file mode 100644 index db0bda3..0000000 --- a/templates/developers.html +++ /dev/null @@ -1,18 +0,0 @@ -= extends "layout.html" - -= block title - About -= endblock - -= block head - {{ assets.tag("developers.css") }} -= endblock - -= block body -
-

Page under construction

-

- Check back later. Have you read our about page and the docs? -

-
-= endblock diff --git a/templates/docs.html b/templates/docs.html index 0ac9bc5..f4b1553 100644 --- a/templates/docs.html +++ b/templates/docs.html @@ -5,6 +5,8 @@ = endblock = block head + {{ assets.tag("lib/highlight.css") }} + {{ assets.tag("docs.css") }} = endblock @@ -21,84 +23,112 @@
  • » Usage

    - bitshift is a search-engine optimized for source code: beyond supporting searches with the full range of ASCII - symbols, the engine understands code, allowing users to query for metadata, like time of creation/last - modification, programming language, and even symbols like function names and variables. Basic use boils down to - general and advanced searches. + bitshift is a search-engine optimized for + source code: beyond supporting searches with the full range of ASCII + symbols, the engine understands code, allowing users to query + for metadata, like time of creation/last modification, programming + language, and even symbols like function names and variables. Basic use + boils down to general and advanced searches.

    diff --git a/templates/layout.html b/templates/layout.html index d5db6ae..b8cd7f5 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -41,7 +41,6 @@ home about docs - developers diff --git a/test/find_function_def.py b/test/find_function_def.py new file mode 100755 index 0000000..187370b --- /dev/null +++ b/test/find_function_def.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +from json import loads +from sys import argv +from urllib import urlencode +from urllib2 import urlopen + +def get_function(name): + params = {"q": "lang:python and func:def:%s" % name} + request = urlopen("http://bitshift.it/search.json?" + urlencode(params)) + res = loads(request.read())["results"] + if res: + print "%s: %s" % (name, res[0]["url"]) + else: + print "%s not found." % name + +if __name__ == "__main__": + if len(argv) == 2: + get_function(argv[1])