%%
%% ---------------------------------------------------------------
%% molc.cbx --- A citation style of manual of law citation
%% modified from gb7714-2015
%% Maintained by huzhenzhen
%% E-mail: hzzmail@163.com
%% Released under the LaTeX Project Public License v1.3c or later
%% ---------------------------------------------------------------
%%
%%用法：类似于标准的样式在biblatex加载时设置样式
%%文献表正常处理，标注命令主要用cite和textcite

\ProvidesFile{chinese-molc.cbx}[v1.0 a biblatex citation style for molc]

%==================================================
%加载gb样式
%==================================================
\RequireCitationStyle{gb7714-2025} %


%==================================================
%选项设置
%==================================================
\ExecuteBibliographyOptions{
    maxcitenames=2,
    mincitenames=1,
    gbfootbibfmt=true
}

%==================================================
%引用命令设置，主要用cite和textcite
%包括标点设置
%==================================================
\DeclareDelimFormat{multicitedelimiter}{\gbpunctsemicolon}%新定义一个multicitedelim来区分不同标注命令下的空格处理

%修改注释后的标点
\renewcommand{\bibfootnotewrapper}[1]{%
  \bibsentence#1}%

%避免去除前面的空白
\renewrobustcmd{\blx@mkbibfootnote}[2]{%
  \iftoggle{blx@footnote}
    {\blx@warning{Nested notes}%
     \addspace\mkbibparens{#2}}
    {%\unspace
     \ifnum\blx@notetype=\tw@
       \expandafter\@firstoftwo
     \else
       \expandafter\@secondoftwo
     \fi
       {\csuse{blx@theendnote#1}{\protecting{\blxmkbibnote{end}{#2}}}}
       {\csuse{footnote#1}{\protecting{\blxmkbibnote{foot}{#2}}}}}}

%修改脚注注释的实现
    %开启引用跟踪计数器，为使用\ifciteseen等测试命令
    \ExecuteBibliographyOptions{citetracker=true}

        %
    %判断当前文献是否已经引用过且是做的footfullcite(即已经作为脚注输出文献内容)
    %记录首次输出时的脚注号码，用于后面再次引用该文献时的输出，比如同(4)。
    %v1.0q 20190309 区分使脚注标注适应minipage中的情况
    \newbibmacro*{citesavefn}{%
        \ifciteseen%当被引用过为true
        {\ifcsdef{sec\arabic{refsection}et\thefield{entrykey}}%当已经定义过脚注序号信息
            {\csgdef{labelsec\arabic{refsection}et\thefield{entrykey}}{%
            \ifciteibid{\iffieldequalstr{userd}{chinese}{同上注}{Ibid.}}%
    {同前注\gdef\@thefnmark{\csuse{sec\arabic{refsection}et\thefield{entrykey}}}%
    \@makefnmark}%
            }}%
            {\csxdef{sec\arabic{refsection}et\thefield{entrykey}}{%
            \ifcsstring{@mpfn}{mpfootnote}{\thempfootnote}{\thefootnote}%
            }}%
        }%
        {\csxdef{sec\arabic{refsection}et\thefield{entrykey}}{%
        \ifcsstring{@mpfn}{mpfootnote}{\thempfootnote}{\thefootnote}%
        %判断在minipage中比较麻烦，这里使用\@mpfn的定义进行判断
        }}%
    }

    %
    %重定义\footfullcite使完成css的脚注文献要求
    \DeclareCiteCommand{\footfullcite}[\mkbibfootnote]%
      {\defcounter{maxnames}{\blx@maxcitenames}%局部定义maxnames和minnames计数器
       \defcounter{minnames}{\blx@mincitenames}%使footfullcite内的作者输出与正文文献表中的一致。
       \usebibmacro{prenote}%
        \renewbibmacro*{postnote}{%
            \iffieldundef{postnote}%
            {}{\setunit{\iffieldequalstr{userd}{chinese}{\unspace\gbpunctcomma}{\addcomma\addspace}}%
            \printfield{postnote}}}%与ay样式不同，由于已修改postnote域格式，直接用printfield
       }%
      {\usebibmacro{citesavefn}%
          \ifcsdef{labelsec\arabic{refsection}et\thefield{entrykey}}%
          {%\csuse{labelsec\arabic{refsection}et\thefield{entrykey}}%
            \ifciteibid{\iffieldequalstr{userd}{chinese}{同上注。}{Ibid.}}%
          {\usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}}%
          }%\nopunct
          {\usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}}%
      }%
      {\multicitedelim}%
      {\usebibmacro{postnote}\usebibmacro{finentry}}


      \DeclareCiteCommand{\footbriefcite}[\mkbibfootnote]%
      {\defcounter{maxnames}{\blx@maxcitenames}%局部定义maxnames和minnames计数器
       \defcounter{minnames}{\blx@mincitenames}%使footfullcite内的作者输出与正文文献表中的一致。
       \usebibmacro{prenote}%
        \renewbibmacro*{postnote}{%
            \iffieldundef{postnote}%
            {}{\setunit{\iffieldequalstr{userd}{chinese}{\unspace\gbpunctcomma}{\addcomma\addspace}}%
            \printfield{postnote}}}%与ay样式不同，由于已修改postnote域格式，直接用printfield
       }%
      {\usebibmacro{citesavefn}%
          \ifcsdef{labelsec\arabic{refsection}et\thefield{entrykey}}%
          {\csuse{labelsec\arabic{refsection}et\thefield{entrykey}}%
          \ifciteibid{}{\usedriver{\DeclareNameAlias{sortname}{default}}{citeseen:\thefield{entrytype}}}%
          }%\nopunct
          {\usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}}%
      }%
      {\multicitedelim}%
      {\usebibmacro{postnote}\usebibmacro{finentry}}


    \DeclareCiteCommand{\fullcite}
      {\usebibmacro{prenote}%
        \renewbibmacro*{postnote}{%
            \iffieldundef{postnote}%
            {}{%\setunit{\iffieldequalstr{userd}{chinese}{\unspace\gbpunctcomma}{\addcomma\addspace}}%
            \printfield{postnote}}}%与ay样式不同，由于已修改postnote域格式，直接用printfield
      }
      {\usebibmacro{citesavefn}%
          \ifcsdef{labelsec\arabic{refsection}et\thefield{entrykey}}%
          {%\csuse{labelsec\arabic{refsection}et\thefield{entrykey}}%
          \usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}%
          }%\nopunct
          {\usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}}%
      }
      {\multicitedelim}
      {\usebibmacro{postnote}\usebibmacro{finentry}}


      \DeclareCiteCommand{\briefcite}
      {\usebibmacro{prenote}%
        \renewbibmacro*{postnote}{%
            \iffieldundef{postnote}%
            {}{%\setunit{\iffieldequalstr{userd}{chinese}{\unspace\gbpunctcomma}{\addcomma\addspace}}%
            \printfield{postnote}}}%与ay样式不同，由于已修改postnote域格式，直接用printfield
      }
      {\usebibmacro{citesavefn}%
          \ifcsdef{labelsec\arabic{refsection}et\thefield{entrykey}}%
          {\csuse{labelsec\arabic{refsection}et\thefield{entrykey}}%
          \usedriver{\DeclareNameAlias{sortname}{default}}{citeseen:\thefield{entrytype}}%
          }%\nopunct
          {\usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}}%
      }
      {\multicitedelim}
      {\usebibmacro{postnote}\usebibmacro{finentry}}


      \DeclareCiteCommand{\fullinnercite}
      {\usebibmacro{prenote}%
        \renewbibmacro*{postnote}{%
            \iffieldundef{postnote}%
            {}{\setunit{\iffieldequalstr{userd}{chinese}{\unspace\gbpunctcomma}{\addcomma\addspace}}%
            \printfield{postnote}}}%与ay样式不同，由于已修改postnote域格式，直接用printfield
      }
      {\ifciteseen%
          {\usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}}%\nopunct
          {\usedriver{\DeclareNameAlias{sortname}{default}}{\thefield{entrytype}}}%
      }
      {\multicitedelim}
      {\usebibmacro{postnote}\usebibmacro{finentry}}



%为先秦文献夹注做处理
\DeclareFieldFormat*{citejz}{%
\iffieldequalstr{userd}{chinese}{\gbpunctprl\gbpunctttl#1\gbpunctttr\gbpunctprr}%
{\mkbibemph{#1}}}
\DeclareCiteCommand{\citejz}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\ifciteindex
     {\indexfield{indextitle}}
     {}%
   \printfield[citejz]{labeltitle}}
  {\multicitedelim}
  {\usebibmacro{postnote}}


\DeclareFieldFormat{citetitle}{%
\iffieldequalstr{userd}{chinese}{\gbpunctttl#1\gbpunctttr}%
{\mkbibemph{#1}}%
}

\DeclareFieldFormat
  [article,inbook,incollection,inproceedings,patent,thesis,unpublished]
  {citetitle}{%
  \iffieldequalstr{userd}{chinese}{\gbpunctttl#1\gbpunctttr}%
  {\mkbibquote{#1\isdot}}%
  }


%为文献输出出版信息
\DeclareCiteCommand{\citepub}
  {\boolfalse{citetracker}%
   \boolfalse{pagetracker}%
   \usebibmacro{prenote}}
  {\ifciteindex
     {\indexfield{indextitle}}
     {}%
    \ifentrytype{online}{\usebibmacro{institution+location+date}}{\usebibmacro{publisher+location+date}}%
   }
  {\multicitedelim}
  {\usebibmacro{postnote}}
