|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +import sys |
| 5 | +import os |
| 6 | +import re |
| 7 | + |
| 8 | +re_md_gist = re.compile(r"@gist\(([^\)]+)\)") |
| 9 | +re_strip = re.compile(r"^\n*(.*)\n\t*$", re.S) |
| 10 | +re_indent = re.compile(r"^(\t*)[^\t\s\n\r]") |
| 11 | +line_start = r"(?:^|\n)\s*" |
| 12 | +re_gist_comment = dict( |
| 13 | + c = dict( |
| 14 | + start = re.compile(r"%s\/\*\s*@gist\s+([\w\-_]+)\s*\*/.*?\n+" % line_start), |
| 15 | + end = re.compile(r"%s\/\*\s*@endgist\s*\*/" % line_start), |
| 16 | + ), |
| 17 | + bash = dict( |
| 18 | + start = re.compile(r"%s#\s*@gist\s+([\w\-_]+).*?\n+" % line_start), |
| 19 | + end = re.compile(r"%s#\s*@endgist" % line_start), |
| 20 | + ), |
| 21 | + cpp = dict( |
| 22 | + start = re.compile(r"%s//\s*@gist\s+([\w\-_]+).*?\n+" % line_start), |
| 23 | + end = re.compile(r"%s//\s*@endgist" % line_start) |
| 24 | + ), |
| 25 | + html = dict( |
| 26 | + start = re.compile(r"%s<!-- @gist\s+([\w\-_]+?) -->.*?\n+" % line_start), |
| 27 | + end = re.compile(r"%s<!-- @endgist -->" % line_start), |
| 28 | + ), |
| 29 | +) |
| 30 | +cpath = sys.path[0] |
| 31 | + |
| 32 | +def openfile(path): |
| 33 | + if not os.path.exists(path): |
| 34 | + return None |
| 35 | + f = open(path, "r") |
| 36 | + body = f.read() |
| 37 | + f.close() |
| 38 | + return body |
| 39 | + |
| 40 | +def get_gist_block(path): |
| 41 | + gists = dict() |
| 42 | + body = openfile(path) |
| 43 | + if body is None: |
| 44 | + return gists |
| 45 | + start = 0 |
| 46 | + while True: |
| 47 | + a = search_one_block(body[start:]) |
| 48 | + if a is None: |
| 49 | + break |
| 50 | + name, content, new_start = a |
| 51 | + start += new_start |
| 52 | + if not name in gists: |
| 53 | + gists[name] = content |
| 54 | + else: |
| 55 | + gists[name].extend(["", "...", ""]) |
| 56 | + gists[name].extend(content) |
| 57 | + gists[""] = body.split("\n") |
| 58 | + return gists |
| 59 | + |
| 60 | +def search_one_block(body): |
| 61 | + if len(body) == 0: |
| 62 | + return None |
| 63 | + for n, regs in re_gist_comment.iteritems(): |
| 64 | + a = regs["start"].search(body) |
| 65 | + if a is None: |
| 66 | + continue |
| 67 | + start = a.span()[1] |
| 68 | + b = regs["end"].search(body[start:]) |
| 69 | + if b is None: |
| 70 | + continue |
| 71 | + break |
| 72 | + if a is None or b is None: |
| 73 | + return None |
| 74 | + |
| 75 | + body = body[start: b.span()[0]+start] |
| 76 | + body = re_strip.sub("\\1", body) |
| 77 | + start_indent = len(re_indent.findall(body)[0]) |
| 78 | + body = [i[start_indent:] for i in body.split("\n")] |
| 79 | + return a.group(1), body, b.span()[1] + start |
| 80 | + |
| 81 | +def dirname(path): |
| 82 | + name = os.path.dirname(path) |
| 83 | + if name == "": |
| 84 | + name = "." |
| 85 | + return name |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + if len(sys.argv) <= 1: |
| 89 | + sys.stderr.write("Usage: %s GistFile > OutputFile\n" % os.path.basename(sys.argv[0])) |
| 90 | + exit(2) |
| 91 | + |
| 92 | + body = openfile(sys.argv[1]) |
| 93 | + if body is None: |
| 94 | + sys.stderr.write("Not such File.") |
| 95 | + exit(2) |
| 96 | + |
| 97 | + rpath = dirname(sys.argv[1]) |
| 98 | + body_gist_ref = [] |
| 99 | + ref_files = [] |
| 100 | + for i in re_md_gist.findall(body): |
| 101 | + file_path = i |
| 102 | + if i.find("#") > 0: |
| 103 | + file_path = file_path.split("#")[0] |
| 104 | + ref_files.append("%s/%s" % (rpath, file_path)) |
| 105 | + body_gist_ref.append(i) |
| 106 | + ref_files = list(set(ref_files)) |
| 107 | + |
| 108 | + match_gists = {} |
| 109 | + for f in ref_files: |
| 110 | + blocks = get_gist_block(f) |
| 111 | + for block_key in blocks: |
| 112 | + key = "%s#%s" % (f, block_key) |
| 113 | + if len(block_key) == 0: |
| 114 | + key = "%s%s" % (f, block_key) |
| 115 | + match_gists[key] = blocks[block_key] |
| 116 | + |
| 117 | + errors = [] |
| 118 | + for i in body_gist_ref: |
| 119 | + key = "%s/%s" % (rpath, i) |
| 120 | + if key in match_gists: |
| 121 | + match_results = re_md_gist.search(body) |
| 122 | + if match_results is None: |
| 123 | + continue |
| 124 | + s = match_results.span()[0] |
| 125 | + s = body[body[s-50: s].rfind("\n")+s-50+1: s] |
| 126 | + content = (("\n%s" % s).join(match_gists[key])).strip() |
| 127 | + content = content.replace("\\", "\\\\") |
| 128 | + |
| 129 | + body = re.sub(r"@gist\s*\(%s\)" % i, content, body) |
| 130 | + else: |
| 131 | + errors.append(i) |
| 132 | + |
| 133 | + if len(errors) > 0: |
| 134 | + sys.stderr.write("error: No Such File or Anchor\n") |
| 135 | + for i, error in enumerate(errors): |
| 136 | + sys.stderr.write("%s: '%s'\n" % (i+1, error)) |
| 137 | + exit(2) |
| 138 | + print body |
0 commit comments