예제 #1
0
def get_doc(docname):
    txt_path = join(DOCS_ROOT, '%s.txt' % docname)
    if not exists(txt_path):
        return None
    html_dir = join(settings.MEDIA_ROOT, 'django_docs')
    if not isdir(html_dir):
        mkdir(html_dir)
    html_path = join(html_dir, '%s.body' % docname)
    html_toc_path = join(html_dir, '%s.toc' % docname)
    # If the HTML exists already (and the text file isn't newer), return it.
    try:
        if getmtime(txt_path) <= getmtime(html_path):
            return {'toc': open(html_toc_path).read(),
                    'html_body': open(html_path).read()}
    except OSError:
        pass
    # Otherwise, convert the RST text file to html...
    txt = open(txt_path).read()
    doc = build_document(txt)
    # ... and write the TOC and html_body sections to disk
    f = open(html_toc_path, 'w')
    f.write(doc['toc'])
    f.close()
    f = open(html_path, 'w')
    f.write(doc['html_body'])
    f.close()
    return {'toc': mark_safe(doc['toc']),
            'html_body': mark_safe(doc['html_body'])}
예제 #2
0
파일: views.py 프로젝트: cstroie/tranpy
def doc_reader(request, topic=None, slug=None, format=None, docs_root=settings.DOCS_ROOT):
    """ Read a document """
    # Define the document name
    if slug is not None:
        doc_name = slug
    else:
        doc_name = settings.DOCS_DEFAULT
    # Get the document topic path
    doc_path = settings.DOCS_TOPIC.get(topic, "tutorial")
    # The path to documentation file
    file_path = join(docs_root, doc_path, "%s.txt" % doc_name)
    # Test the file exists
    if not exists(file_path):
        return page_not_found(request, message=_("The required document page does not exist."))
    # Cache key
    cache_key = "tranpy:docs:%s:%s:%s" % (topic, doc_name, getmtime(file_path))
    # Try to read from cache
    parts = cache.get(cache_key)
    if parts is None:
        # Not found in cache
        parts = build_document(file_path)
        cache.set(cache_key, parts, 60 * 60)
    # Define the template
    if format == "xml":
        template_name = "docs/minimal.html"
    else:
        template_name = "docs/detail.html"
    # Return the response
    return render_to_response(
        template_name,
        {"doc": parts, "topic": topic, "slug": slug, "format": format},
        context_instance=RequestContext(request),
    )