%%%%%%----------------------------------------------------
%%%%%%	Package:	affilauthor
%%%%%%	Author:		Selvam P.
%%%%%%	Mail:		selvamittex@gmail.com
%%%%%%	Version:	1.0.1 - 2023/11/29
%%%%%%	Licence: 	Released under the LaTeX Project Public License v1.3c or later, 
%%%%%%				see http://www.latex-project.org/lppl.txt					 
%%%%%%	Description: key-value style fields to tag the author and affiliation informations in a structured format. 
%%%%%%					Each field has a specific name within \author and \affil commands similar to bib format. 
%%%%%%					We can customize the styles as per preferences for article.cls class layout. 
%%%%%%					Instead of giving all information(author and affiliation) in single tag, we can split the information in a format of key-value style. 
%%%%%%----------------------------------------------------
\NeedsTeXFormat{LaTeX2e}[1995/06/01]
\ProvidesPackage{affilauthor}[2023/11/29 1.0.1 key value style for author names]

\RequirePackage{marvosym}

\ExplSyntaxOn

\tl_new:N \l__affauth_tmpa_tl
\tl_new:N \l__affauth_tmpb_tl
\int_new:N \l__affauth_tmpa_int
\int_new:N \l__affauth_tmpb_int
\bool_new:N \l__affauth_tmpa_bool
\bool_new:N \l__affauth_tmpb_bool
\seq_new:N \l__affauth_tmpa_seq
\seq_new:N \l__affauth_tmpb_seq

\tl_new:N \l__affauth_auth_name_tl
\tl_new:N \l__affauth_auth_mail_tl
\tl_new:N \l__affauth_auth_phone_tl
\tl_new:N \l__affauth_auth_url_tl
\tl_new:N \l__affauth_auth_note_tl
\tl_new:N \l__affauth_auth_affil_id_tl

\tl_new:N \l__affauth_font_author_tl
\tl_new:N \l__affauth_font_affil_tl
\tl_new:N \l__affauth_font_mail_tl
\tl_new:N \l__affauth_font_url_tl
\tl_new:N \l__affauth_font_phone_tl
\tl_new:N \l__affauth_font_note_tl
\tl_new:N \l__affauth_font_note_num_tl

\tl_new:N \l__affauth_aff_id_tl
\tl_new:N \l__affauth_aff_div_tl
\tl_new:N \l__affauth_aff_org_tl
\tl_new:N \l__affauth_aff_addr_tl
\tl_new:N \l__affauth_aff_street_tl
\tl_new:N \l__affauth_aff_landmark_tl
\tl_new:N \l__affauth_aff_pincode_tl
\tl_new:N \l__affauth_aff_postbox_tl
\tl_new:N \l__affauth_aff_city_tl
\tl_new:N \l__affauth_aff_state_tl
\tl_new:N \l__affauth_aff_country_tl

\seq_new:N \l__affauth_author_info_seq
\prop_new:N \l__affauth_author_id_prop
\seq_new:N \l__affauth_affil_info_seq
\int_new:N \l__affauth_author_cnt_int
\int_new:N \l__affauth_affil_cnt_int
\int_new:N \l__affauth_author_note_int
\prop_new:N \l__affauth_author_note_prop
\prop_new:N \l__affauth_author_contact_prop

\skip_new:N \__affauth_before_affil_skip
\skip_new:N \__affauth_before_auth_skip

\skip_set:Nn \__affauth_before_affil_skip {2\baselineskip}
\skip_set:Nn \__affauth_before_auth_skip {0pt}

\keys_define:nn { author-info }
  {
    name .tl_set:N   = \l__affauth_name_tl,
    mail .tl_set:N   = \l__affauth_auth_mail_tl,  
    url .tl_set:N   = \l__affauth_auth_url_tl,  
    phone .tl_set:N  = \l__affauth_auth_phone_tl,  
    note .tl_set:N   = \l__affauth_auth_note_tl,        
    affil .tl_set:N  = \l__affauth_auth_affil_id_tl    
  }

\keys_define:nn { affil-info }
  {
    id .tl_set:N        = \l__affauth_aff_id_tl,    
    div .tl_set:N       = \l__affauth_aff_div_tl,
    org .tl_set:N       = \l__affauth_aff_org_tl,  
    addr .tl_set:N      = \l__affauth_aff_addr_tl,  
    street .tl_set:N    = \l__affauth_aff_street_tl,  
    landmark .tl_set:N  = \l__affauth_aff_landmark_tl,        
    pincode .tl_set:N   = \l__affauth_aff_pincode_tl,    
    postbox .tl_set:N   = \l__affauth_aff_postbox_tl,      
    city .tl_set:N      = \l__affauth_aff_city_tl,      
    state .tl_set:N     = \l__affauth_aff_state_tl,      
    country .tl_set:N   = \l__affauth_aff_country_tl      
  }

\keys_define:nn { style-format }
  {
    authfont .tl_set:N    = \l__affauth_font_author_tl,
    affilfont .tl_set:N   = \l__affauth_font_affil_tl,  
    mailfont .tl_set:N    = \l__affauth_font_mail_tl,  
    urlfont .tl_set:N     = \l__affauth_font_url_tl,  
    phonefont .tl_set:N   = \l__affauth_font_phone_tl,  
    notefont .tl_set:N    = \l__affauth_font_note_tl,        
    affilnum .choices:nn   = { alpha, Alpha, roman, Roman }
    {
      \int_case:nn {\l_keys_choice_int}
        {
          {1}{\let\l__affauth_aff:n\@alph}
          {2}{\let\l__affauth_aff:n\@Alph}          
          {3}{\let\l__affauth_aff:n\@roman}                    
          {4}{\let\l__affauth_aff:n\@Roman}                              
        }
    },    
    notenum .choices:nn   = { alpha, fnsym, roman }
    {
      \int_case:nn {\l_keys_choice_int}
        {
          {1}{\let\l__affauth_note:n\@alph}
          {2}{\let\l__affauth_note:n\@fnsymbol}          
          {3}{\let\l__affauth_note:n\@roman}                    
        }
    },
    authspace. skip_set:N   = \__affauth_before_auth_skip,    
    affilspace. skip_set:N  = \__affauth_before_affil_skip
  }
  

\NewDocumentCommand{\affilstyle}{O{}} {\keys_set:nn { style-format } { #1 }}

\cs_set_protected:Nn \__affauth_aff_id:n 
  {
    \group_begin:
      \seq_set_split:Nxx \l__affauth_tmpa_seq {,} {#1}
      \seq_clear:N \l__affauth_tmpb_seq
      \seq_map_inline:Nn \l__affauth_tmpa_seq 
        {
          \prop_if_in:NeT \l__affauth_author_id_prop {##1}
            {
              \prop_get:NeN \l__affauth_author_id_prop {##1} \l__affauth_tmpb_tl
              \seq_put_right:NV \l__affauth_tmpb_seq \l__affauth_tmpb_tl
            }
        }
      \seq_sort:Nn \l__affauth_tmpb_seq
        {
          \int_compare:nNnTF {##1} < {##2}
            {\sort_return_same:}{\sort_return_swapped:}
        }
        \seq_clear:N \l__affauth_tmpa_seq
        \seq_map_inline:Nn \l__affauth_tmpb_seq
          {
              \seq_put_right:Ne \l__affauth_tmpa_seq {\l__affauth_aff:n{##1}}
          }
        \__affauth_sups:n{\seq_use:Nn \l__affauth_tmpa_seq {,}}
    \group_end:
  }

\cs_set:Nn \__affauth_auth_sep:n {}
\cs_set:Nn \l__affauth_aff:n {#1}
\cs_set_protected:Nn \__affauth_sups:n {\textsuperscript{#1}}

\cs_set_protected:Nn \__affauth_aff_number:n 
  {
    \int_compare:nNnT {#1} > {1} {\par}
    \__affauth_sups:n{\l__affauth_aff:n{#1}}\thinspace
  }


\cs_set_protected:Nn \l__affauth_note:n {\ifcase#1\or *\or
  **\or***\or****\or\#\or\#\#\or\#\#\#\or\#\#\#\#\or\$\or\$\$\or\$\$\$\or\$\$\$\$
  \or\dag\or\dag\dag\or\dag\dag\dag\or\dag\dag\dag\dag\or\ddag\or\ddag\ddag\or\ddag\ddag\ddag\or\ddag\ddag\ddag\ddag\fi}

\cs_set_protected:Nn \__affauth_auth_mail:nnnnn
  {
    \group_begin:
      \seq_clear:N \l__affauth_tmpa_seq
      \seq_clear:N \l__affauth_tmpb_seq
      \seq_set_split:Nee \l__affauth_tmpb_seq {,} {#5}      
      \tl_if_empty:nF {#1} {\seq_put_right:Nn \l__affauth_tmpa_seq {{\l__affauth_font_mail_tl\Letter\thinspace\exp_not:n{#1}}}}
      \tl_if_empty:nF {#2} {\seq_put_right:Nn \l__affauth_tmpa_seq {{\l__affauth_font_phone_tl\Telefon\thinspace\exp_not:n{#2}}}}
      \tl_if_empty:nF {#3} {\seq_put_right:Nn \l__affauth_tmpa_seq {{\l__affauth_font_url_tl\Mundus\thinspace\exp_not:n{#3}}}}    
      \seq_if_empty:NF \l__affauth_tmpa_seq      
        {
          \group_begin:
            \prop_gput:Nee \l__affauth_author_contact_prop {\seq_item:Nn \l__affauth_tmpb_seq {1}} {\seq_use:Nn \l__affauth_tmpa_seq {,\space}}
          \group_end:
        }
      \tl_if_empty:nF {#4} 
      {
        \group_begin:
          \int_gincr:N \l__affauth_author_note_int
          \__affauth_sups:n{,\l__affauth_note:n{\l__affauth_author_note_int}}
          \prop_gput:Nee \l__affauth_author_note_prop {\seq_item:Nn \l__affauth_tmpb_seq {1}} {\l__affauth_note:n{\int_use:N \l__affauth_author_note_int}\exp_not:n{#4}}          
        \group_end:
      }                 
    \group_end:
  }

\cs_set_protected:Npn \author #1
  {
    \group_begin:
      \int_gincr:N \l__affauth_author_cnt_int
      \keys_set:nn { author-info } { #1 } 
      \seq_gput_right:Ne \l__affauth_author_info_seq 
        {
          \l__affauth_name_tl
          \exp_args:NV \__affauth_aff_id:n \l__affauth_auth_affil_id_tl
          \__affauth_auth_mail:nnnnn {\l__affauth_auth_mail_tl} {\l__affauth_auth_phone_tl} {\l__affauth_auth_url_tl} {\l__affauth_auth_note_tl}{\l__affauth_auth_affil_id_tl}                                
          \exp_args:NV \__affauth_auth_sep:n \l__affauth_author_cnt_int                
        }   
    \group_end:
  }

\cs_set_protected:Npn \affil #1
  {
    \group_begin:
      \int_gincr:N \l__affauth_affil_cnt_int
      \keys_set:nn { affil-info } { #1 } 
      \prop_gput:NVV \l__affauth_author_id_prop \l__affauth_aff_id_tl \l__affauth_affil_cnt_int
      \prop_gput:NVV \l__affauth_author_id_prop \l__affauth_affil_cnt_int \l__affauth_aff_id_tl      
      \seq_gput_right:Ne \l__affauth_affil_info_seq 
        {
          \exp_args:NV \__affauth_aff_number:n \l__affauth_affil_cnt_int
          \tl_if_empty:NF \l__affauth_aff_div_tl \l__affauth_aff_div_tl
          \tl_if_empty:NF \l__affauth_aff_org_tl {,\space} \l__affauth_aff_org_tl
          \tl_if_empty:NF \l__affauth_aff_addr_tl {,\space} \l__affauth_aff_addr_tl
          \tl_if_empty:NF \l__affauth_aff_street_tl {,\space} \l__affauth_aff_street_tl
          \tl_if_empty:NF \l__affauth_aff_landmark_tl {,\space} \l__affauth_aff_landmark_tl
          \tl_if_empty:NF \l__affauth_aff_pincode_tl {,\space} \l__affauth_aff_pincode_tl                                        
          \tl_if_empty:NF \l__affauth_aff_postbox_tl {,\space} \l__affauth_aff_postbox_tl        
          \tl_if_empty:NF \l__affauth_aff_city_tl {,\space} \l__affauth_aff_city_tl                  
          \tl_if_empty:NF \l__affauth_aff_state_tl {,\space} \l__affauth_aff_state_tl        
          \tl_if_empty:NF \l__affauth_aff_country_tl {,\space} \l__affauth_aff_country_tl                        
        }
    \group_end:
  }

\let\org@maketitle=\maketitle
\def\maketitle
  {{\renewenvironment{tabular}[2][]{\begin{center}}
    {\end{center}}
    \org@maketitle}
  }

\cs_set_protected:Npn \__show_affiliation: 
  {
    \group_begin:
    \int_zero:N \l__affauth_affil_cnt_int
      \l__affauth_font_affil_tl
      \seq_map_inline:Nn \l__affauth_affil_info_seq 
        {
          \int_incr:N \l__affauth_affil_cnt_int
          ##1\par
          \prop_get:NVN \l__affauth_author_id_prop \l__affauth_affil_cnt_int \l__affauth_tmpa_tl          
          \prop_if_in:NVT \l__affauth_author_contact_prop \l__affauth_tmpa_tl
            {
              (\prop_item:NV \l__affauth_author_contact_prop \l__affauth_tmpa_tl)
            }
            \prop_if_in:NVT \l__affauth_author_note_prop \l__affauth_tmpa_tl
            {
              \footnotetext[0]{\kern-\parindent\prop_item:NV \l__affauth_author_note_prop \l__affauth_tmpa_tl}
            }            
        }
    \group_end:
  }

\cs_set_protected:Nn \__show_authors: 
  {
    \group_begin:
    \l__affauth_font_author_tl
    \seq_use:Nnnn \l__affauth_author_info_seq {\space and\space}{,\space}{,\space and\space}
    \group_end:
  }


\AtBeginDocument{
  \gdef\@author
    {
      \group_begin:
        \vskip\__affauth_before_auth_skip
        \__show_authors:\par
        \vskip\__affauth_before_affil_skip
        \__show_affiliation: 
      \group_end:
    }
  }
\ExplSyntaxOff