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 |
- Check back later. Have you read our about page and the docs? -
-- 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.
- To perform a "general search," simply place your cursor in the search bar on our home page and begin - entering text; when you stop typing for a short period of time, we'll automatically execute the query for you. - As you scroll down the results page, new codelets will be seamlessly downloaded from our server and - appended to the end. + To perform a "general search," simply place your cursor in the + search bar on our home page and begin entering text; when you + stop typing for a short period of time, we'll automatically + execute the query for you. As you scroll down the page, new + codelets, or results, will be seamlessly downloaded from our + server and appended to the end.
- General searches, though, are limited. To allow users to make the best of our engine, we created an advanced - search form that allows the creation of complex queries with the following specifiers: + General searches, though, are limited. To allow users to make the + best of our engine, we created an advanced search form that + allows the creation of complex queries with the following + specifiers:
- Each of the search fields allows for numerous values; just separate them with spaces. If you'd like - to search for a multi-word, space-delimited string, on the other hand, enclose it in double quotes. + Each of the search fields allows for numerous values; just + separate them with spaces. If you'd like to search for a + multi-word, space-delimited string, on the other hand, + enclose it in double quotes. - A query for foo bar will search for occurrences of both "foo" and "bar", while "foo bar" - will search for occurrences of "foo bar". + A query for foo bar will search + for occurrences of both "foo" and + "bar", while + "foo bar" will search for + occurrences of "foo bar".
- Search groups facilitate even more robust queries: they're like a bunch of individual searches - grouped into one. A user searching for occurrenes of symbol "curses" in the - language "Python", and "ncurses" in "C", won't get away with: symbols: "curses ncurses" and - languages: "Python C". The engine might return results "curses" in "C" and "ncurses" in - "Python"! - - To work around that, you can use two search groups: one for "curses" in - "Python", and another for "curses" in "C". bitshift will return the union of both sets of search results. + Search groups facilitate even more robust queries: they're + like a bunch of individual searches grouped into one. A + user searching for occurrenes of symbol + "curses" in the language + "Python", and + "ncurses" in + "C", won't get away with: + "symbols:curses ncurses" and + "languages:Python C". The engine + might return results "curses" in + "C" and + "ncurses" in + "Python"! + + To work around that, you can use two search groups: one for + "curses" in + "Python", and another for + "curses" in + "C". + bitshift will return the union + of both sets of search results.
+ bitshift provides an API through GET + requests to + /search.json. +
++ /search.json returns a JSON-encoded + dictionary. If there was an error, it will contain a single key, + "error", whose value will contain a + human-readable description of the error. Otherwise, there will be two + keys: "count", storing the number of results, + and "results", storing a list of codelets. + Each codelet is a dictionary with the following key–value pairs: +
++ The following example Python 2 code searches for a given Python + function definition and prints the URL of the first result: +
+
+
+
+ 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 + 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]) ++ |
+
- bitshift is (gasp) open-source! The project is hosted on - GitHub; feel free to file an issue or submit a pull request! + bitshift is (gasp) + open-source! The project is hosted on + GitHub; feel free to + file an issue or submit a pull request.