# Copyright (C) 2006, 2007, 2008  Andrey Golovizin
#
# This file is part of pybtex.
#
# pybtex is free software; you can redistribute it and/or modify
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# pybtex is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pybtex; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
# USA

"""name formatting styles
"""
from pybtex.richtext import Symbol, Text, nbsp
from pybtex.style.template import join, together, node, _format_list
from pybtex.bibtex.name import tie_or_space

@node
def name_part(children, data, before='', tie=False):
    parts = together [children].format_data(data)
    if not parts:
        return Text()
    if tie:
        return Text(before, parts, tie_or_space(parts, nbsp, ' '))
    else:
        return Text(before, parts)


def plain(person, abbr=False):
    r"""
    >>> from pybtex.core import Person
    >>> name = Person(string=r"Charles Louis Xavier Joseph de la Vall{\'e}e Poussin")
    >>> print plain(name).format().plaintext()
    de<nbsp>la Vall{\'e}e<nbsp>Poussin, Charles Louis Xavier<nbsp>Joseph
    >>> print plain(name, abbr=True).format().plaintext()
    de<nbsp>la Vall{\'e}e<nbsp>Poussin, C.<nbsp>L. X.<nbsp>J.
    >>> name = Person(first='First', last='Last', middle='Middle')
    >>> print plain(name).format().plaintext()
    Last, First<nbsp>Middle
    >>> print plain(name, abbr=True).format().plaintext()
    Last, F.<nbsp>M.

    """
    return join [
        name_part(tie=True) [person.prelast()],
        name_part [person.last()],
        name_part(before=', ') [person.lineage()],
        name_part(before=', ') [person.first(abbr) + person.middle(abbr)],
    ]

@@ -1263,24 +1263,50 @@
 ):%0A    r%22%22%22%0A
+    %7Bff~%7D%7Bvv~%7D%7Bll%7D%7B, jj%7D%0A%0A
     %3E%3E%3E from
@@ -1332,16 +1332,16 @@
  Person%0A
-
     %3E%3E%3E 
@@ -1457,32 +1457,903 @@
 t().plaintext()%0A
+    Charles Louis Xavier%3Cnbsp%3EJoseph de%3Cnbsp%3Ela Vall%7B%5C'e%7De%3Cnbsp%3EPoussin%0A    %3E%3E%3E print plain(name, abbr=True).format().plaintext()%0A    C.%3Cnbsp%3EL. X.%3Cnbsp%3EJ. de%3Cnbsp%3Ela Vall%7B%5C'e%7De%3Cnbsp%3EPoussin%0A%0A    %3E%3E%3E name = Person(first='First', last='Last', middle='Middle')%0A    %3E%3E%3E print plain(name).format().plaintext()%0A    First%3Cnbsp%3EMiddle Last%0A    %3E%3E%3E print plain(name, abbr=True).format().plaintext()%0A    F.%3Cnbsp%3EM. Last%0A    %22%22%22%0A    return join %5B%0A        name_part(tie=True) %5Bperson.first(abbr) + person.middle(abbr)%5D,%0A        name_part(tie=True) %5Bperson.prelast()%5D,%0A        name_part %5Bperson.last()%5D,%0A        name_part(before=', ') %5Bperson.lineage()%5D%0A    %5D%0A%0Adef last_first(person, abbr=False):%0A    r%22%22%22%0A    %3E%3E%3E from pybtex.core import Person%0A    %3E%3E%3E name = Person(string=r%22Charles Louis Xavier Joseph de la Vall%7B%5C'e%7De Poussin%22)%0A    %3E%3E%3E print last_first(name).format().plaintext()%0A
     de%3Cnbsp%3Ela V
@@ -2415,37 +2415,42 @@
 h%0A    %3E%3E%3E print 
-p
 la
-in
+st_first
 (name, abbr=True
@@ -2534,16 +2534,17 @@
 nbsp%3EJ.%0A
+%0A
     %3E%3E%3E 
@@ -2608,37 +2608,42 @@
 )%0A    %3E%3E%3E print 
-p
 la
-in
+st_first
 (name).format().
@@ -2688,37 +2688,42 @@
 e%0A    %3E%3E%3E print 
-p
 la
-in
+st_first
 (name, abbr=True

Rename names.plain to last_first, add a real plain style ({ff~}{vv~}{ll}{, jj}).