示例#1
0
def createmonthlyindex(indexmarkup, monthindexpath):
    '''Create a monthly index when it doesn't exist.'''
    # Code ici pour lire un fichier avec des variables
    # substituer les variables par les valeurs du mois
    # sauver le fichier au bon endroit
    msg = "Do not forget to update /map with your tiny hands"
    logging.info("%s" % (msg))

    with open(TEMPLATEDIR + 'index-mois.html', 'r') as source:
        t = string.Template(source.read())
        datestring = nowdate(DATENOW, 'iso')
        datehumain = nowdate(DATENOW, 'humain')
        # to get month, we split in 3 the human date and take the second
        # argument
        datemois = datehumain.split(' ')[1]
        indexli = etree.tostring(
            indexmarkup, pretty_print=True, encoding='utf-8')
        result = t.substitute(isodateshort=datestring,
                              monthname=datemois,
                              year=datestring[:4],
                              humandate=datehumain,
                              firstentry=indexli)
        # need to write it on the filesystem.
    with open(monthindexpath, 'w') as monthindex:
        monthindex.write(result)
示例#2
0
def last_posts_html(entries):
    '''Return the HTML markup for the last entries.'''
    # msg = "Generating HTML markup for last entries in the feed"
    # logging.info("%s" % (msg))
    last_posts_markup = ""
    with open(TEMPLATEDIR + 'last_posts.html', 'r') as source:
        t = string.Template(source.read())
        for i, entry in enumerate(entries):
            updated = entry['updated']
            hupdated = nowdate(rfc3339_to_datetime(updated),
                               'humain').decode('utf-8')
            published = entry['published']
            hpublished = nowdate(rfc3339_to_datetime(published), 'humain')
            last_posts_markup += t.substitute(
                ttitle=entry['title'].decode('utf-8'),
                turl=entry['url'],
                tupdated=updated,
                tupdated_human=hupdated,
                tpublished=published,
                tpublished_human=hpublished.decode('utf-8'))
    return last_posts_markup
示例#3
0
def main():
    '''The core task for processing a file for La Grange.'''
    # Logging File Configuration
    logging.basicConfig(filename='log-ymir.txt', level=logging.DEBUG,
                        format='%(asctime)s - %(levelname)s - %(message)s')
    logging.info('-'*80)
    # Command Line Interface
    parser = argparse.ArgumentParser(
        description="Managing Web site blog posts")
    parser.add_argument('rawpost', metavar='FILE', help='file to be processed',
                        action='store', nargs=1, type=argparse.FileType('rt'))
    args = parser.parse_args()
    # Arguments attribution
    rawpostpath = args.rawpost[0]
    # PATH CONFIGURATIONS
    # Getting the path of the current post on the OS
    abspathpost = os.path.abspath(rawpostpath.name)
    post_directory = os.path.dirname(abspathpost)
    # Finding the root of the Web site
    site_root = find_root(post_directory, ROOT_TOKEN)
    # Post path and full URL without ".html"
    postpath = abspathpost[len(site_root):]
    posturl = "%s%s" % (SITE[:-1], postpath[:-5])
    # Feed
    feed_path = '%s/%s' % (site_root, FEEDATOMNOM)
    # Site Home Page
    home_path = '%s/%s' % (site_root, 'index.html')
    # Monthly index
    monthabspath = os.path.dirname(os.path.dirname(abspathpost))
    monthindexpath = monthabspath + "/index.html"
    # BACKUPS?
    # preparing places for backup
    BACKUP_PATH = '/tmp/lagrange'
    if not os.path.isdir(BACKUP_PATH):
        os.mkdir(BACKUP_PATH)
    feed_path_bkp = '%s/%s' % (BACKUP_PATH, FEEDATOMNOM)
    shutil.copy(feed_path, feed_path_bkp)
    # PROCESSING
    # Parse the document
    rawpost = parserawpost(rawpostpath)
    # Extracting Post Information
    titlemarkup, title = gettitle(rawpost)
    title = title.decode("utf-8").strip()
    logging.info("TITLE: %s" % (title))
    created = getdocdate(rawpost, 'created')
    logging.info("CREATED: %s" % (created))
    modified = getdocdate(rawpost, 'modified')
    DATENOW = rfc3339_to_datetime(modified)
    logging.info("MODIFIED: %s" % (modified))
    content = getcontent(rawpost)

    # INDEX MARKUP
    indexmarkup = createindexmarkup(postpath[:-5], created, title)
    # Create the monthly index if it doesn't exist yet
    # Happen once a month
    if not os.path.isfile(monthindexpath):
        createmonthlyindex(indexmarkup, monthindexpath)
    else:
        # TOFIX: updating the monthly index
        # updatemonthlyindex(indexmarkup, monthindexpath)
        print(etree.tostring(indexmarkup, pretty_print=True, encoding='utf-8'))

    # FEED ENTRY MARKUP
    tagid = createtagid(posturl, created)
    # UPDATING FEED
    feedentry = makefeedentry(
        posturl, tagid, title, created, nowdate(DATENOW, 'rfc3339'), content)
    feed_content = update_feed(feedentry, feed_path_bkp)
    if feed_content:
        with open(feed_path, 'w') as feedbkp:
            feedbkp.write(feed_content)
    # UPDATING HOME PAGE
    home_content = update_home_index(feed_path, home_path)
    with open(home_path, 'w') as home:
        home.write(home_content)
    # UPDATING MONTHLY INDEX
    updatemonthlyindex(indexmarkup, monthindexpath)