from django.conf import settings

from share.normalize import *  # noqa


class Person(Parser):
    given_name = ParseName(ctx.name).first
    family_name = ParseName(ctx.name).last
    additional_name = ParseName(ctx.name).middle
    suffix = ParseName(ctx.name).suffix
    affiliations = ctx.maybe('arxiv:affiliation')('*')


class Contributor(Parser):
    order_cited = ctx('index')
    person = ctx
    cited_name = ctx.name


class CreativeWork(Parser):
    title = ctx.entry.title
    description = ctx.entry.summary
    contributors = ctx.entry.author('*')
    published = ctx.entry.published
    # doi = settings.DOI_BASE_URL + ctx.entry.maybe('arxiv:doi')['#text']
    subject = ctx.entry['arxiv:primary_category']
    tags = ctx.entry.category('*')


class Affiliation(Parser):
    organization = ctx


class Organization(Parser):
    name = ctx['#text']


class Tag(Parser):
    name = ctx['@term']
    type = ctx['@term']


class ThroughTags(Parser):
    tag = ctx


class Taxonomy(Parser):
    name = ctx

@@ -67,16 +67,108 @@
  noqa%0A%0A%0A
+class Organization(Parser):%0A    name = ctx%5B'#text'%5D%0A%0A%0Aclass Affiliation(Parser):%0A    pass%0A%0A%0A
 class Pe
@@ -379,18 +379,89 @@
 s = 
-ctx.maybe(
+Map(%0A        Delegate(Affiliation.using(entry=Organization)),%0A        Maybe(ctx, 
 'arx
@@ -476,20 +476,21 @@
 iation')
-('*'
+%0A    
 )%0A%0A%0Aclas
@@ -598,150 +598,370 @@
 ass 
-CreativeWork(Parser):%0A    title = ctx.entry.title%0A    description = ctx.entry.summary%0A    contributors = ctx.entry.author('*')%0A    published =
+Taxonomy(Parser):%0A    name = ctx%0A%0A%0Aclass Tag(Parser):%0A    name = ctx%5B'@term'%5D%0A    type = Delegate(Taxonomy, ctx%5B'@term'%5D)%0A%0A%0Aclass ThroughTags(Parser):%0A    tag = Delegate(Tag, ctx)%0A%0A%0Aclass Preprint(Parser):%0A    title = ctx.entry.title%0A    description = ctx.entry.summary%0A    published = ParseDate(ctx.entry.published)%0A    contributors = Map(Delegate(Contributor),
  ctx
@@ -967,25 +967,23 @@
 x.entry.
-published
+author)
 %0A    # d
@@ -1114,287 +1114,51 @@
 s = 
-ctx.entry.category('*')%0A%0A%0Aclass Affiliation(Parser):%0A    organization = ctx%0A%0A%0Aclass Organization(Parser):%0A    name = ctx%5B'#text'%5D%0A%0A%0Aclass Tag(Parser):%0A    name = ctx%5B'@term'%5D%0A    type = ctx%5B'@term'%5D%0A%0A%0Aclass ThroughTags(Parser):%0A    tag = ctx%0A%0A%0Aclass Taxonomy(Parser):%0A    name = ctx
+Map(Delegate(ThroughTags), ctx.entry.category)
 %0A

Update arxiv normalizers