| 21 | | def build(dirs): |
|---|
| | 22 | MODEL_DOC_TEMPLATE = """ |
|---|
| | 23 | <div class="document" id="model-{{ model_name }}"> |
|---|
| | 24 | |
|---|
| | 25 | <h1 class="title">{{ title }}</h1> |
|---|
| | 26 | {{ blurb }} |
|---|
| | 27 | |
|---|
| | 28 | <h2 id='model-source-code'>Model source code</h2> |
|---|
| | 29 | <pre class="literal-block">{{ model_source }}</pre> |
|---|
| | 30 | |
|---|
| | 31 | <h2 id='api-reference'>API reference</h2> |
|---|
| | 32 | |
|---|
| | 33 | {% for model in models %} |
|---|
| | 34 | <h3>{{ model.name }} objects have the following methods:</h3> |
|---|
| | 35 | <ul> |
|---|
| | 36 | {% for method in model.methods %}<li><tt class="docutils literal"><span class="pre">{{ method }}()</span></tt></li> |
|---|
| | 37 | {% endfor %}</ul> |
|---|
| | 38 | {% endfor %} |
|---|
| | 39 | |
|---|
| | 40 | <h2 id='sample-usage'>Sample API usage</h2> |
|---|
| | 41 | <pre class="literal-block">{{ api_usage }}</pre> |
|---|
| | 42 | </div> |
|---|
| | 43 | """ |
|---|
| | 44 | |
|---|
| | 45 | MODEL_TOC = """ |
|---|
| | 46 | <ul> |
|---|
| | 47 | <li><a href="#model-source-code">Model source code</a></li> |
|---|
| | 48 | <li><a href="api-reference">API reference</a></li> |
|---|
| | 49 | <li><a href="sample-usage">Sample API usage</a></li> |
|---|
| | 50 | </ul> |
|---|
| | 51 | """ |
|---|
| | 52 | |
|---|
| | 53 | def build_documents(): |
|---|
| 23 | | for dir in dirs: |
|---|
| 24 | | for fname in glob.glob1(dir, "*.txt"): |
|---|
| 25 | | in_file = os.path.join(dir, fname) |
|---|
| 26 | | out_file = os.path.join(dir, os.path.splitext(fname)[0] + ".html") |
|---|
| 27 | | toc_file = os.path.join(dir, os.path.splitext(fname)[0] + "_toc.html") |
|---|
| 28 | | parts = publish_parts( |
|---|
| 29 | | open(in_file).read(), |
|---|
| 30 | | source_path=in_file, |
|---|
| 31 | | destination_path=out_file, |
|---|
| 32 | | writer=writer, |
|---|
| 33 | | settings_overrides=SETTINGS, |
|---|
| 34 | | ) |
|---|
| 35 | | open(out_file, 'w').write(parts['html_body']) |
|---|
| 36 | | open(toc_file, 'w').write(parts['toc']) |
|---|
| | 55 | for fname in glob.glob1(settings.DJANGO_DOCUMENT_ROOT_PATH, "*.txt"): |
|---|
| | 56 | in_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, fname) |
|---|
| | 57 | out_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, os.path.splitext(fname)[0] + ".html") |
|---|
| | 58 | toc_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, os.path.splitext(fname)[0] + "_toc.html") |
|---|
| | 59 | parts = publish_parts( |
|---|
| | 60 | open(in_file).read(), |
|---|
| | 61 | source_path=in_file, |
|---|
| | 62 | destination_path=out_file, |
|---|
| | 63 | writer=writer, |
|---|
| | 64 | settings_overrides=SETTINGS, |
|---|
| | 65 | ) |
|---|
| | 66 | open(out_file, 'w').write(parts['html_body']) |
|---|
| | 67 | open(toc_file, 'w').write(parts['toc']) |
|---|
| | 68 | |
|---|
| | 69 | def build_test_documents(): |
|---|
| | 70 | sys.path.append(settings.DJANGO_TESTS_PATH) |
|---|
| | 71 | writer = DjangoHTMLWriter() |
|---|
| | 72 | import runtests |
|---|
| | 73 | |
|---|
| | 74 | # Manually set INSTALLED_APPS to point to the test app. |
|---|
| | 75 | settings.INSTALLED_APPS = (runtests.APP_NAME,) |
|---|
| | 76 | |
|---|
| | 77 | for model_name in runtests.get_test_models(): |
|---|
| | 78 | mod = meta.get_app(model_name) |
|---|
| | 79 | |
|---|
| | 80 | out_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, 'model_' + model_name + '.html') |
|---|
| | 81 | toc_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, 'model_' + model_name + '_toc.html') |
|---|
| | 82 | |
|---|
| | 83 | # Clean up the title and blurb. |
|---|
| | 84 | title, blurb = mod.__doc__.strip().split('\n', 1) |
|---|
| | 85 | parts = publish_parts( |
|---|
| | 86 | blurb, |
|---|
| | 87 | source_path=mod.__file__, |
|---|
| | 88 | destination=out_file, |
|---|
| | 89 | writer=writer, |
|---|
| | 90 | settings_overrides=SETTINGS, |
|---|
| | 91 | ) |
|---|
| | 92 | blurb = parts["html_body"] |
|---|
| | 93 | api_usage = mod.API_TESTS |
|---|
| | 94 | |
|---|
| | 95 | # Get the source code of the model, without the docstring or the |
|---|
| | 96 | # API_TESTS variable. |
|---|
| | 97 | model_source = inspect.getsource(mod) |
|---|
| | 98 | model_source = model_source.replace(mod.__doc__, '') |
|---|
| | 99 | model_source = model_source.replace(mod.API_TESTS, '') |
|---|
| | 100 | model_source = model_source.replace('""""""\n', '\n') |
|---|
| | 101 | model_source = model_source.replace('API_TESTS = ', '') |
|---|
| | 102 | model_source = model_source.strip() |
|---|
| | 103 | |
|---|
| | 104 | models = [] |
|---|
| | 105 | for m in mod._MODELS: |
|---|
| | 106 | models.append({ |
|---|
| | 107 | 'name': m._meta.object_name, |
|---|
| | 108 | 'methods': [method for method in dir(m) if not method.startswith('_')], |
|---|
| | 109 | }) |
|---|
| | 110 | |
|---|
| | 111 | # Run this through the template system. |
|---|
| | 112 | t = template.Template(MODEL_DOC_TEMPLATE) |
|---|
| | 113 | c = template.Context(locals()) |
|---|
| | 114 | html = t.render(c) |
|---|
| | 115 | |
|---|
| | 116 | try: |
|---|
| | 117 | fp = open(out_file, 'w') |
|---|
| | 118 | except IOError: |
|---|
| | 119 | sys.stderr.write("Couldn't write to %s.\n" % file_name) |
|---|
| | 120 | continue |
|---|
| | 121 | else: |
|---|
| | 122 | fp.write(html) |
|---|
| | 123 | fp.close() |
|---|
| | 124 | |
|---|
| | 125 | try: |
|---|
| | 126 | fp = open(toc_file, 'w') |
|---|
| | 127 | except IOError: |
|---|
| | 128 | sys.stderr.write("Couldn't write to %s.\n" % file_name) |
|---|
| | 129 | continue |
|---|
| | 130 | else: |
|---|
| | 131 | fp.write(MODEL_TOC) |
|---|
| | 132 | fp.close() |
|---|