1. Ali Gholami Rudi
  2. noor

Source

noor / noorcgi.py

#!/usr/bin/env python
import cgi
import ConfigParser
import os
import sys
import cgitb
cgitb.enable()

# Add noor to the python path if its not installed:
sys.path.append('/home/ali/projects/noor')
import noor.uihelpers


def write_aya(text, number=None, sajda=None, translation=None, juz_start=None):
    result = []
    if juz_start is not None:
        result.append('<font color="green" size=+2><b>[%s]'
                      '</b></font><br>\n' % juz_start)
    result.append('<big>%s</big>' % text)

    if number is not None:
        sign = str(number)
        if sajda == 'minor':
            sign = '<font color="red">*%s*</font>' % sign
        if sajda == 'major':
            sign = '<font color="red">**%s**</font>' % sign
        result.append('<font color="blue">[%s]</font><br>\n' % sign)
    else:
        result.append('<br>\n')
    if translation is not None:
        result.append('<small>%s</small>\n' % translation)
    result.append('<hr>\n')
    return ''.join(result)


def handle_request(query_string=None):
    result = []
    result.append('<?xml version="1.0" encoding="utf-8"?>\n')
    result.append(
        '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n')
    result.append('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n')
    result.append('<html xmlns="http://www.w3.org/1999/xhtml" '
                  'xml:lang="en" lang="en" dir="rtl">\n')

    query = {}
    if query_string is not None:
        query = cgi.parse_qs(query_string)

    quran = noor.uihelpers.get_quran()
    config = noor.uihelpers.get_config()
    trans = noor.uihelpers.get_translation(config)

    result.append(_get_font_tag(config))
    if 'sura' in query:
        sura_number = int(query['sura'][0])
        sura_text = noor.uihelpers.write_sura(quran, sura_number,
                                              write_aya, trans)
        sura = quran.suras[sura_number - 1]
        result.append('<head><title>Noor - %d. %s</title></head>' %
                      (sura_number, sura.name))
        result.append(sura_text)
    else:
        result.append('<head><title>Noor - Sura List</title></head>')
        result.append(_get_font_tag(config))
        result.append('<table border=1>\n')
        result.append('<tr><td>Index</td><td>Sura Name</td>'
                      '<td>Aya Count</td></tr>\n')
        for index, sura in enumerate(quran.suras):
            sura_name = '<a href="noorcgi.py?sura=%s">%s</a>' % (index + 1,
                                                                 sura.name)
            result.append('<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n' %
                          (index + 1, sura_name, len(sura.ayas)))
        result.append('</table>\n')
        result.append('</font>')
        result.append('<hr>Generated by <a href="http://noor.sf.net/">Noor</a>')

    result.append('</font>\n')
    result.append('</html>\n')
    return(''.join(result).encode('utf-8'))


def _get_font_tag(config):
    attrs = []
    try:
        tokens = config.get('cgi', 'font').split()
        if tokens[-1].isdigit():
            attrs.append(('size', tokens[-1]))
            del tokens[-1]
        if tokens:
            attrs.insert(0, ('face', ' '.join(tokens)))
    except ConfigParser.Error:
        attrs.append(('face', 'nazli'))
    return '<font %s >' % ' '.join(('%s="%s"' % (attr, value))
                                   for attr, value in attrs)


print 'Content-Type: text/html'
print
query_string = os.environ.get('QUERY_STRING', None)
print handle_request(query_string)