Q:

How to automatically tag chapters,sections,sub..sections and paragraphs?

I want to do a package that handles the automatic tagging of pdf documents. I try to solve the problem of taggings paragraphs with chapters, sections, subsections, subsubsections, ..., sub..sections and paragraphs with subparagraphs. I read the example of tagpdf package that performs tagging of chapters, sections, sub..sections etc., but first it only works for the names of those parts of the document and the text of the document that are not displayed in pdf, and secondly it only works for the scrbook class, but I want, if it is possible, that it works for any class of documents (I do not know if it is possible to check the class of the document via latex/lualatex). I tried to use \everypar commands, but as I understood, reading my log, it seems that this command is called only n+1 times, where n is the number of sections and sub...sections. My document now only needs to be compiled in lualatex. Thanks very much everybody for the help.
test_tagging_of_pars.tex
\documentclass[12pt]{scrbook}
\usepackage{tagpdf}
\tagpdfsetup{interwordspace=true,activate-all,uncompress}
\usepackage{amsmath,amssymb}
\title{test document}
\author{AlexanderKozlovskiy}
\date{\today}
%\maketitle (why this not works?)
%Marking the toc entries
%around the whole entry so only structure:
\newcommand\tagscrtocentry[1]{\tagstructbegin{tag=TOCI}#1\tagstructend}

%leaf so structure and mc:
\newcommand\tagscrtocpagenumber[1]{%
 \tagstructbegin{tag=Reference}%
 \tagmcbegin{tag=Reference}%
 #1%
 \tagmcend
 \tagstructend}

\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{chapter}
\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{section}
\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{subsection}
\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{subsubsection}
\DeclareTOCStyleEntry[
   entryformat=\tagscrtocentry,
   pagenumberformat=\tagscrtocpagenumber]{tocline}{paragraph}

\renewcommand{\addtocentrydefault}[3]{%
 \ifstr{#3}{}{}
   {%\
   \ifstr{#2}{}
    {%
     \addcontentsline{toc}{#1}
      {%
       \protect\nonumberline
       \tagstructbegin{tag=P}%
       \tagmcbegin{tag=P}%
        #3%
       \tagmcend
       \tagstructend
      }%
    }%
    {%
    \addcontentsline{toc}{#1}{%
     \tagstructbegin{tag=Lbl}%
     \tagmcbegin{tag=Lbl}%
     \protect\numberline{#2}%
     \tagmcend\tagstructend
     \tagstructbegin{tag=P}%
     \tagmcbegin{tag=P}%
      #3%
     \tagmcend
     \tagstructend
     }%
    }%
   }}%

% the dots must be marked too
\makeatletter
\renewcommand*{\TOCLineLeaderFill}[1][.]{%
  \leaders\hbox{$\m@th
    \mkern \@dotsep mu\hbox{\tagmcbegin{artifact}#1\tagmcend}\mkern \@dotsep
    mu$}\hfill
}

%%%%%%%%%
% Sectioning commands
%%%%%%%%

\ExplSyntaxOn
\prop_new:N   \g_tag_section_level_prop
\prop_gput:Nnn \g_tag_section_level_prop {chapter}{H1}
\prop_gput:Nnn \g_tag_section_level_prop {section}{H2}
\prop_gput:Nnn \g_tag_section_level_prop {subsection}{H3}
\prop_gput:Nnn \g_tag_section_level_prop {subsubsection}{H4}
\prop_gput:Nnn \g_tag_section_level_prop {paragraph}{H5}

%new 0.6, as attributes are local we have to put \tagmcbegin everywhere.
\renewcommand{\chapterlinesformat}[3]
 {
  \@hangfrom
   {
    \tagstructbegin{tag=\prop_item:Nn\g_tag_section_level_prop{chapter}}
    \tl_if_empty:nF{#2}
     {
      \tagmcbegin    {tag=\prop_item:Nn\g_tag_section_level_prop{chapter}}
      #2
      \tagmcend
     }
   }
   {\tagmcbegin    {tag=\prop_item:Nn\g_tag_section_level_prop{chapter}}
    #3\tagmcend\tagstructend}%
}

%unnumbered sections level give an empty mc, need to think about it.
\renewcommand{\sectionlinesformat}[4]
 {
  \@hangfrom
   {\hskip #2
    \tagstructbegin{tag=\prop_item:Nn\g_tag_section_level_prop{#1}}
    \tl_if_empty:nF{#3}
    {
     \tagmcbegin    {tag=\prop_item:Nn\g_tag_section_level_prop{#1}}
     #3
     \tagmcend
    }
   }
   {\tagmcbegin    {tag=\prop_item:Nn\g_tag_section_level_prop{#1}}
    #4
    \tagmcend\tagstructend}%
 }
\ExplSyntaxOff
\AfterTOCHead{\tagstructbegin{tag=TOC}}
\AfterStartingTOC{\tagstructend} %end TOC

\begin{document}
\tagstructbegin{tag=Document}
%do tagging of paragraphs
\ExplSyntaxOn
\everypar{
\message{the_size_of_stack_of_structure_elements_is_\seq_count:N \g__uftag_struct_stack_seq} %i dont know,why spaces ignore when i try input something in log,so i use _ instead of space character.
\int_case:nn {\seq_count:N \g__uftag_struct_stack_seq}
  {
   {2}{\tagstructbegin{tag=P}\tagmcbegin{tag=P}}
{4}{\tagstructend \tagstructbegin{tag=P}\tagmcbegin{tag=P}}}}
\ExplSyntaxOff
\begin{centering}
test tagging of parts of documents\\
\end{centering}

\newpage

\tableofcontents

\newpage

\chapter{first chapter}

start testing of tagging of paragraphs

\section{test of section}

{\tiny

this is test document,which allow to do tests of tagging sections and paragraphs

\subsection{subsection 1}

test

again test

\begin{description}

\item[1] lemon

\item[2] orange

again testing of tagging parts of document

\item[3] red

\item[4] green

\end{description}}

\newpage

\subsection{new test}

end of test of tagging of document.

\ExplSyntaxOn
\int_step_inline:nnn{2}{\seq_count:N \g__uftag_struct_stack_seq }
{\tagstructend}
\ExplSyntaxOff
\end{document}

A:

There is currently no easy way to do it "for all sorts of documents" as
LaTeX and almost all classes and relevant packages are missing suitable interfaces.
Solving this is not easy. In view of the variety of implementations in this area I doubt that it will possible to solve this by some changes in the latex kernel alone.
To add the structure to a \section for one of the standard classes you could redefine the internal \@sect command of the latex kernel. But this would have no effect for classes from the KOMA-bundle or memoir or revtex4-1 and more as all either ignore or overwrite the command. 
For \part and \chapter there is nothing in the kernel anyway - every class has its own implementation. 
So my long term plan here is currently to develop a suitable hook system and then to convince all major classes and packages to add these hooks at the right places. Then a tagging package could simply add the tagging commands to these hooks. 
I do not plan to extend the tagpdf package with patches for all sort of  classes and packages to get this working. The few examples are only examples to demonstrate that it is possible to get a structure. 

