必要なのは、複数行のテキストをフォーマットするための texttable というライブラリだと思います。そして、正規表現を使用して装飾を必要なものに置き換えることができます。
__author__ = 'mmyjona'
#coding=utf-8
import pprint
from texttable import Texttable
import yaml
pp = pprint.PrettyPrinter(indent=4)
#set the width here
MAX_WIDTH=50
raw = """
header:
main_title: title text
subtitle: subtitle text
date: 2012-11-13
entries:
- image: 1.jpg
title: title1
body: Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
- image: 2.jpg
title: title2
body: Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
footer:
text: i'm footer
"""
stream = yaml.load(raw)
pp.pprint(stream)
tar = """
############################
# HEADER #
# ======================== #
# IMAGE | Title #
# | Description #
# ------------------------ #
# ... #
# ------------------------ #
# IMAGE | Title #
# | Description #
# ======================== #
# FOOTER #
############################
"""
table1 = Texttable()
table1.__init__(max_width=MAX_WIDTH)
table1.set_chars(['=', '|', '+', '-'])
table1.set_cols_align(["r", "l"])
table1.set_cols_valign(["t", "t"])
table1.add_rows([["foo","bar"]
,["Main Title",stream['header']['main_title']+"\n"]
,["Subtitle",stream['header']['subtitle']+"\n"]
,["Date",str(stream['header']['date'])+"\n"]
])
for x in stream['entries']:
table1.add_row([x['image'],x['title'] + "\n" + x['body']])
table1.add_row(['Footer', stream['footer']['text']])
print table1.draw() + "\n"
出力:
{ 'entries': [ { 'body': 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.',
'image': '1.jpg',
'title': 'title1'},
{ 'body': 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.',
'image': '2.jpg',
'title': 'title2'}],
'footer': { 'text': "i'm footer"},
'header': { 'date': datetime.date(2012, 11, 13),
'main_title': 'title text',
'subtitle': 'subtitle text'}}
+=======================+=======================+
| foo | bar |
+-----------------------+-----------------------+
| Main Title | title text |
+=======================+=======================+
| Subtitle | subtitle text |
+=======================+=======================+
| Date | 2012-11-13 |
+=======================+=======================+
| 1.jpg | title1 |
| | Lorem ipsum dolor sit |
| | amet, consectetuer |
| | adipiscing elit, sed |
| | diam nonummy nibh |
| | euismod tincidunt ut |
| | laoreet dolore magna |
| | aliquam erat |
| | volutpat. Ut wisi |
| | enim ad minim veniam, |
| | quis nostrud exerci |
| | tation ullamcorper |
| | suscipit lobortis |
| | nisl ut aliquip ex ea |
| | commodo consequat. |
+=======================+=======================+
| 2.jpg | title2 |
| | Lorem ipsum dolor sit |
| | amet, consectetuer |
| | adipiscing elit, sed |
| | diam nonummy nibh |
| | euismod tincidunt ut |
| | laoreet dolore magna |
| | aliquam erat |
| | volutpat. Ut wisi |
| | enim ad minim veniam, |
| | quis nostrud exerci |
| | tation ullamcorper |
| | suscipit lobortis |
| | nisl ut aliquip ex ea |
| | commodo consequat. |
+=======================+=======================+
| Footer | i'm footer |
+=======================+=======================+