template.render(Context({'tag': tag, 'category': _(tag.category)})) for tag in tags))
     
 
-
 def simple_title(tags):
     title = []
     for tag in tags:
             %(children)s
             """ % {
                 "depth": depth,
-                "title": book.title, 
+                "title": book.title,
                 "audiences": ", ".join(book.audiences_pl()),
                 "audiobook": "audiobook" if book.has_media('mp3') else "",
                 "children": book_tree_texml(books_by_parent.get(book.id, ()), books_by_parent, depth + 1)
             } for book in book_list)
 
 
+@register.simple_tag
+def book_tree_csv(author, book_list, books_by_parent, depth=1, max_depth=3, delimeter="\t"):
+    def quote_if_necessary(s):
+        try:
+            s.index(delimeter)
+            s.replace('"', '\\"')
+            return '"%s"' % s
+        except ValueError:
+            return s
+        
+    return "".join("""%(author)s%(d)s%(preindent)s%(title)s%(d)s%(postindent)s%(audiences)s%(d)s%(audiobook)s
+%(children)s""" % {
+                "d": delimeter,
+                "preindent": delimeter * (depth - 1),
+                "postindent": delimeter * (max_depth - depth), 
+                "depth": depth,
+                "author": quote_if_necessary(author.name),
+                "title": quote_if_necessary(book.title),
+                "audiences": ", ".join(book.audiences_pl()),
+                "audiobook": "audiobook" if book.has_media('mp3') else "",
+                "children": book_tree_csv(author, books_by_parent.get(book.id, ()), books_by_parent, depth + 1)
+            } for book in book_list)
+
 @register.simple_tag
 def all_editors(extra_info):
     editors = []
 
--- /dev/null
+{% load catalogue_tags %}{% book_tree_texml "" orphans books_by_parent %}{% for author, group in books_by_author.items %}{% if group %}{% book_tree_csv author group books_by_parent %}{% endif %}{% endfor %}
 
                     <cmd name="name"><parm>{{ author }}</parm></cmd>
                     <ctrl ch='\' />
 
-                    {% book_tree_texml group books_by_parent %}
+                    {% book_tree_texml author group books_by_parent %}
                 {% endif %}
             {% endfor %}
         </TeXML>
 
 urlpatterns = patterns('reporting.views',
     url(r'^$', 'stats_page', name='reporting_stats'),
     url(r'^katalog.pdf$', 'catalogue_pdf', name='reporting_catalogue_pdf'),
+    url(r'^katalog.csv$', 'catalogue_csv', name='reporting_catalogue_csv'),
 )
 
 
         shutil.rmtree(tempdir)
 
 
+def render_to_csv(output_path, template, context=None, add_files=None):
+    """Renders a TeXML document into a PDF file.
+
+    :param str output_path: is where the PDF file should go
+    :param str template: is a TeXML template path
+    :param context: is context for rendering the template
+    :param dict add_files: a dictionary of additional files XeTeX will need
+    """
+
+    from django.template.loader import render_to_string
+    
+    try:
+        os.makedirs(os.path.dirname(output_path))
+    except:
+        pass
+
+    rendered = render_to_string(template, context)
+    with open(output_path, 'w') as csv_file:
+        csv_file.write(rendered.encode('utf-8'))
+        
+
 def read_chunks(f, size=8192):
     chunk = f.read(size)
     while chunk:
 
 from django.template import RequestContext
 
 from catalogue.models import Book, BookMedia
-from reporting.utils import render_to_pdf, generated_file_view
+from reporting.utils import render_to_pdf, render_to_csv, generated_file_view
 
 
 @staff_member_required
     render_to_pdf(path, 'reporting/catalogue.texml', locals(), {
             "wl-logo.png": os.path.join(settings.STATIC_ROOT, "img/logo-big.png"),
         })
+
+
+@generated_file_view('reports/katalog.csv', 'application/csv', 
+        send_name=lambda: 'wolnelektury_%s.csv' % date.today(),
+        signals=[Book.published])
+def catalogue_csv(path):
+    books_by_author, orphans, books_by_parent = Book.book_list()
+    render_to_csv(path, 'reporting/catalogue.csv', locals())