
from rinoh.document import DocumentSection
from rinoh.paragraph import Paragraph

from .base import ContentsPart, DocumentBase, DocumentOptions


# document parts
# ----------------------------------------------------------------------------

class ArticlePart(ContentsPart):
    def new_page(self, chains):
        page = super().new_page(chains)
        if self.number_of_pages == 0:
            meta = self.document.metadata
            page.content << Paragraph(meta['title'], style='title')
            page.content << Paragraph(meta['subtitle'], style='subtitle')
            date = meta['date']
            try:
                page.content << Paragraph(date.strftime('%B %d, %Y'),
                                          style='author')
            except AttributeError:
                page.content << Paragraph(date, style='author')
            page.content << Paragraph(meta['author'], style='author')
        return page


class ArticleSection(DocumentSection):
    parts = [ArticlePart]


# main document
# ----------------------------------------------------------------------------

class ArticleOptions(DocumentOptions):
    pass


class Article(DocumentBase):
    sections = [ArticleSection]
    options_class = ArticleOptions

@@ -79,317 +79,232 @@
 aph%0A
-%0Afrom .base import ContentsPart, DocumentBase, DocumentOptions%0A%0A%0A# document parts%0A# ----------------------------------------------------------------------------%0A%0Aclass ArticlePart(ContentsPart):%0A    def new_page(self, chains):%0A        page = super().new_page(chains)%0A        if self.number_of_pages == 0:%0A    
+from rinoh.structure import GroupedFlowables, Section, Heading, TableOfContents%0A%0Afrom .base import ContentsPart, DocumentBase, DocumentOptions%0A%0A%0Aclass ArticleFrontMatter(GroupedFlowables):%0A    def flowables(self, document):%0A
     
@@ -318,13 +318,8 @@
 a = 
-self.
 docu
@@ -340,35 +340,21 @@
         
-    page.content %3C%3C
+yield
  Paragra
@@ -394,35 +394,56 @@
         
-    page.content %3C%3C
+if 'subtitle' in meta:%0A            yield
  Paragra
@@ -473,32 +473,59 @@
 yle='subtitle')%0A
+        if 'date' in meta:%0A
             date
@@ -569,39 +569,29 @@
             
-page.content %3C%3C
+yield
  Paragraph(d
@@ -620,50 +620,8 @@
 Y'),
-%0A                                         
  sty
@@ -680,39 +680,29 @@
             
-page.content %3C%3C
+yield
  Paragraph(d
@@ -734,27 +734,46 @@
     
-    page.content %3C%3C
+if 'author' in meta:%0A            yield
  Par
@@ -815,26 +815,435 @@
 r')%0A
-        return pag
+%0A%0A# document parts%0A# ----------------------------------------------------------------------------%0A%0Aclass ArticlePart(ContentsPart):%0A    def flowables(self):%0A        yield ArticleFrontMatter()%0A        yield Section(%5BHeading('Table of Contents', style='unnumbered'),%0A                       TableOfContents()%5D,%0A                      style='table of contents')%0A        for flowable in super().flowables():%0A            yield flowabl
 e%0A%0A%0A

add front matter (title, author, toc, ...)