org2any.py 3.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
"""Org-mode module providing facilities to export Org documents or 
   parts of Org documents to a variety of other formats.

   This exports:
   - org2any.org2html: Export as a HTML-formatted data. 

"""

# Imports required
from tempfile import NamedTemporaryFile
from subprocess import call
import urllib

14 15
from gnowsys_ndf.settings import EMACS_INIT_FILE_PATH

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
###########################################################################

def org2html(org_content, file_prefix="", file_delete=True):
    """org2html(org_content[, file_prefix=""[, file_delete=True]])

    Description: 
    Creates a temporary org-file with given content having default 
    file-name and extension ".org"; and if file_prefix provided it's prefixed 
    to the default file-name. Based on that file, a temporary html file is 
    created and from that file only the content between the "body" element 
    tag, is stripped off and returned to the calling function.

    NOTE: This function deletes "org" file (only when 'file_delete' is set 
    to 'True'), and not the "Html" file.

    Example: 
    For an Org file [myfile-]temp2.org, the HTML file will be 
    [myfile-]temp2.html. The file will be overwritten without warning.

    Arguments:
    org_content - a string representing org-mode content
    file_prefix[optional] - required to create temporary file with the given prefix
    file_delete[optional] - whether to delete the temporary file once it is closed or not 

40
    Returns: a unicode representing org-mode content exported to HTML-formatted content. 
41 42

    """
43
            
44
    # org editor content manipulation for temporary file (".org")
45 46
    org_content_header_for_file = "\n#+OPTIONS: timestamp:nil author:nil creator:nil ^:{} H:3 num:nil toc:nil @:t ::t |:t ^:t -:t f:t *:t <:t" \
                                + "\n#+TITLE: \n"
Avadoot Nachankar's avatar
Avadoot Nachankar committed
47

48
    org_content_for_file = org_content.replace("\r", "")
49 50 51 52 53 54 55 56

    # Creating a temporary file with ".org" extension 
    file_suffix=".org"        # ".org" suffix must; otherwise emacs command won't work!
    org_file_obj = NamedTemporaryFile('w+t', suffix=file_suffix, prefix=file_prefix, delete=file_delete)

    filename_org = org_file_obj.name
    # Example (filename_org): "/tmp/wikiname-usrname-tmptCd4aq.org"

Avadoot Nachankar's avatar
Avadoot Nachankar committed
57 58 59
    encode_content = (org_content_header_for_file + org_content_for_file).encode('utf-8')

    org_file_obj.write(encode_content)
60 61 62 63 64 65 66
    # NOTE: Don't close this file till the time, html file is created

    # Move the cursor - pointing to start of the file
    # Must, otherwise cursor remains @ end (as this file isn't closed after writing data into it) and you won't get any data
    org_file_obj.seek(0)

    # Exporting the above created ".org" file to html
67
    # Example (filename_html): "/tmp/wiliname-usrname-tmptCd4aq.html"
68 69 70
    ext_html = ".html"
    filename_html = filename_org[:-4] + ext_html
    
71
    cmd = "emacs -l " + EMACS_INIT_FILE_PATH  + " --batch " + filename_org + " --eval '(org-export-as-html nil)'"
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    cmd_res = call((cmd + ' </dev/null'), shell=True)
            
    # Close ".org" temporary file
    org_file_obj.close()
            
    if cmd_res <= 0:
        print "\n\n Html file created @ {0}\n".format(filename_html)
    else:
        # TODO: Throw error, may be I/O
        print "\n\n Some error occurred!!!\n"
        
    # Reading data from ".html" file and pasting it in wikipage object's "content" variable 
    with open(filename_html, 'r') as html_file_obj:
        html_data = html_file_obj.readlines()

    # Stripping and storing html data i.e. between <body>...</body> (Both elements are not included)
    strip_html_data = ""
    start_index = html_data.index("<body>\n") + 1         # Start copying data after <body>\n element
    end_index = html_data.index("</body>\n")              # Copy data until you reach before </body>\n element
    for line in html_data[start_index:end_index]:
92
        strip_html_data += line.decode('utf-8').lstrip()
93 94

    return strip_html_data