{%- set indent_space = '  ' -%}
{%- set margin_space = '　' -%}

{%- macro m_law(law) -%}
{%- for el in law.children -%}
  {%- if el.tag == 'LawBody' -%}
    {{- m_law_body(el) -}}
  {%- elif el.tag == 'LawNum' -%}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_law_body(law_body) -%}
{%- for el in law_body.children -%}
  {%- if el.tag == 'LawTitle' -%}
    {{- m_law_title(el) -}}
  {%- elif el.tag == 'EnactStatement' -%}
    {{- m_enact_statement(el) -}}
  {%- elif el.tag == 'TOC' -%}
    {{- m_toc(el) -}}
  {%- elif el.tag in ['MainProvision', 'SupplProvision'] -%}
    {{- m_article_group(el) -}}
  {%- elif el.tag == 'AppdxTable' -%}
    {{- m_appdx_table(el) -}}
  {%- elif el.tag == 'AppdxStyle' -%}
    {{- m_appdx_style(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_law_title(law_title) -%}
{{- m_text(law_title) -}}
{#- new #}
{# line -#}
{%- for el in law.children -%}
  {%- if el.tag == 'LawNum' -%}
    （{{- m_text(el) -}}）
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_enact_statement(enact_statement) -%}
{#- new #}
{# line -#}
{{- m_text(enact_statement) -}}
{#- new #}
{# line -#}
{%- endmacro -%}



{%- macro m_toc(toc) -%}
{#- new #}
{# line -#}
{%- for el in toc.children -%}
  {%- if el.tag == 'TOCLabel' -%}
    {{- m_text(el) -}}
{#- new #}
{# line -#}
  {%- elif el.tag in ['TOCPart', 'TOCChapter', 'TOCSection', 'TOCSupplProvision'] -%}
    {{- m_toc_group(el, 1) -}}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_toc_group(toc_group, indent, no_newline) -%}
{%- for el in toc_group.children -%}
  {%- if el.tag in ['TOCChapter', 'TOCSection', 'TOCSubsection', 'TOCDivision'] -%}
  {%- elif el.tag in [
          'SupplProvisionLabel',
          'PartTitle', 'ChapterTitle', 'SectionTitle',
          'SubsectionTitle', 'DivisionTitle'
      ] -%}
    {%- for _ in range(indent) -%}
      {{- indent_space -}}
    {%- endfor -%}
    {{- m_text(el) -}}
  {%- elif el.tag == 'ArticleRange' -%}
    {{- m_text(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{#- new #}
{# line -#}
{%- for el in toc_group.children -%}
  {%- if el.tag in ['TOCChapter', 'TOCSection', 'TOCSubsection', 'TOCDivision'] -%}
    {{- m_toc_group(el, indent + 1, loop.last) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_article_group(article_group) -%}
{%- for el in article_group.children -%}
  {%- if el.tag in ['Part', 'Chapter', 'Section', 'Subsection', 'Division'] -%}
    {{- m_article_group(el) -}}
  {%- elif el.tag in [
          'SupplProvisionLabel',
          'PartTitle', 'ChapterTitle', 'SectionTitle',
          'SubsectionTitle', 'DivisionTitle'
      ] -%}
{#- new #}
{# line -#}
    {%- set title_indent = {
            'SupplProvisionLabel': 3,
            'PartTitle': 2, 'ChapterTitle': 3, 'SectionTitle': 4,
            'SubsectionTitle': 5, 'DivisionTitle': 6
        } -%}
    {%- for _ in range(title_indent[el.tag]) -%}
      {{- indent_space -}}
    {%- endfor -%}
    {{- m_text(el) -}}
    {%- if 'AmendLawNum' in article_group.attr -%}
      （{{- article_group.attr['AmendLawNum'] -}}）
    {%- endif -%}
    {%- if article_group.attr['Extract'] == 'true' -%}
      {{- margin_space -}}抄
    {%- endif -%}
{#- new #}
{# line -#}
  {%- elif el.tag == 'Article' -%}
    {{- m_article(el) -}}
  {%- elif el.tag == 'Paragraph' -%}
    {{- m_paragraph_item(el, 0) -}}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_article(article) -%}
{#- new #}
{# line -#}
{%- for el in article.children -%}
  {%- if el.tag == 'ArticleCaption' -%}
    {{- indent_space -}}{{- m_text(el) -}}
{#- new #}
{# line -#}
  {%- elif el.tag == 'ArticleTitle' -%}
    {{- m_text(el) -}}
  {%- elif el.tag == 'Paragraph' -%}
    {{- m_paragraph_item(el, 0, article) -}}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_paragraph_item(paragraph_item, indent, article) -%}
{{- context.set('paragraph_item_title_exists', 0) -}}
{%- for el in paragraph_item.children -%}
  {%- if el.tag == 'ParagraphCaption' -%}
{#- new #}
{# line -#}
    {{- indent_space -}}{{- m_text(el) -}}
{#- new #}
{# line -#}
  {%- elif el.tag in [
          'ParagraphNum', 'ItemTitle',
          'Subitem1Title', 'Subitem2Title', 'Subitem3Title', 'Subitem4Title',
          'Subitem5Title', 'Subitem6Title', 'Subitem7Title', 'Subitem8Title',
          'Subitem9Title', 'Subitem10Title'
      ] -%}
    {%- for _ in range(indent) -%}
      {{- indent_space -}}
    {%- endfor -%}
    {%- if el.children | length -%}
      {{- m_text(el) -}}
      {{- context.set('paragraph_item_title_exists', 1) -}}
    {%- endif -%}
  {%- elif el.tag in [
          'ParagraphSentence', 'ItemSentence',
          'Subitem1Sentence', 'Subitem2Sentence', 'Subitem3Sentence', 'Subitem4Sentence',
          'Subitem5Sentence', 'Subitem6Sentence', 'Subitem7Sentence', 'Subitem8Sentence',
          'Subitem9Sentence', 'Subitem10Sentence'
      ] -%}
    {%- set sentence_text = m_paragraph_item_sentence(el, indent) -%}
    {%- if sentence_text | trim | length -%}
      {%- if context.get('paragraph_item_title_exists') or article -%}
        {{- margin_space -}}
      {%- else -%}
{#- new #}
{# line -#}
      {%- endif -%}
      {{- sentence_text -}}
    {%- else -%}
{#- new #}
{# line -#}
    {%- endif -%}
  {%- elif el.tag in [
          'Item',
          'Subitem1', 'Subitem2', 'Subitem3', 'Subitem4',
          'Subitem5', 'Subitem6', 'Subitem7', 'Subitem8',
          'Subitem9', 'Subitem10'
      ] -%}
    {{- m_paragraph_item(el, indent + 1) -}}
  {%- elif el.tag == 'TableStruct' -%}
    {{- m_table_struct(el, indent + 1) -}}
  {%- elif el.tag == 'List' -%}
    {{- m_list(el, indent + 2) -}}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_paragraph_item_sentence(paragraph_item_sentence, indent) -%}
{%- for el in paragraph_item_sentence.children -%}
  {%- if el.tag == 'Sentence' -%}
    {{- m_text(el) -}}
  {%- elif el.tag == 'Column' -%}
    {{- m_column(el) -}}
    {%- if not loop.last -%}
      {{- margin_space -}}
    {%- endif -%}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{#- new #}
{# line -#}
{%- endmacro -%}



{%- macro m_column(column) -%}
{%- for el in column.children -%}
  {%- if el.tag == 'Sentence' -%}
    {{- m_text(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_table_struct(table_struct, indent) -%}
{%- for el in table_struct.children -%}
  {%- if el.tag == 'Table' -%}
    {{- m_table(el, indent) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_table(table, indent) -%}
{%- for el in table.children -%}
  {%- if el.tag == 'TableRow' -%}
    {{- m_table_row(el, indent) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_table_row(table_row, indent) -%}
{%- for el in table_row.children -%}
  {%- if el.tag == 'TableColumn' -%}
    {{- m_table_column(el, indent, loop.first) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_table_column(table_column, indent, first_column) -%}

{%- for el in table_column.children -%}
  {%- if el.tag == 'Sentence' -%}
    {%- for _ in range(indent) -%}
      {{- indent_space -}}
    {%- endfor -%}

    {%- if loop.first -%}
      {%- if first_column -%}
        {{- '* - ' -}}
      {%- else -%}
        {{- '  - ' -}}
      {%- endif -%}

      {%- if 'rowspan' in table_column.attr -%}
        [rowspan="{{- table_column.attr['rowspan'] -}}"]
      {%- endif -%}
      {%- if 'colspan' in table_column.attr -%}
        [colspan="{{- table_column.attr['colspan'] -}}"]
      {%- endif -%}
      {%- if 'Align' in table_column.attr -%}
        [Align="{{- table_column.attr['Align'] -}}"]
      {%- endif -%}
      {%- if 'Valign' in table_column.attr -%}
        [Valign="{{- table_column.attr['Valign'] -}}"]
      {%- endif -%}
    {%- else -%}
      {{- '    ' -}}
    {%- endif -%}

    {{- m_text(el) -}}
{#- new #}
{# line -#}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_style_struct(style_struct, indent) -%}
{%- for el in style_struct.children -%}
  {%- if el.tag == 'StyleStructTitle' -%}
    {%- for _ in range(indent) -%}
      {{- indent_space -}}
    {%- endfor -%}
    {{- m_text(el) -}}
{#- new #}
{# line -#}
  {%- elif el.tag == 'Style' -%}
    {{- m_style(el, indent) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_style(style, indent) -%}
{%- for el in style.children -%}
  {%- if el.tag == 'Fig' -%}
    {{- m_fig(el, indent) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_appdx_table(appdx_table) -%}
{#- new #}
{# line -#}
{%- for el in appdx_table.children -%}
  {%- if el.tag == 'AppdxTableTitle' -%}
    {{- m_text(el) -}}
    {%- for el2 in appdx_table.children -%}
      {%- if el2.tag == 'RelatedArticleNum' -%}
        {{- m_text(el2) -}}
      {%- endif -%}
    {%- endfor -%}
{#- new #}
{# line -#}
  {%- elif el.tag == 'RelatedArticleNum' -%}
  {%- elif el.tag == 'TableStruct' -%}
    {{- m_table_struct(el, 1) -}}
  {%- elif el.tag == 'Item' -%}
    {{- m_paragraph_item(el, 1) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_appdx_style(appdx_style) -%}
{#- new #}
{# line -#}
{%- for el in appdx_style.children -%}
  {%- if el.tag == 'AppdxStyleTitle' -%}
    {{- m_text(el) -}}
    {%- for el2 in appdx_style.children -%}
      {%- if el2.tag == 'RelatedArticleNum' -%}
        {{- m_text(el2) -}}
      {%- endif -%}
    {%- endfor -%}
{#- new #}
{# line -#}
  {%- elif el.tag == 'RelatedArticleNum' -%}
  {%- elif el.tag == 'StyleStruct' -%}
    {{- m_style_struct(el, 1) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_list(list, indent) -%}
{%- for el in list.children -%}
  {%- if el.tag == 'ListSentence' -%}
    {{- m_list_sentence(el, indent) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_list_sentence(list_sentence, indent) -%}
{%- for el in list_sentence.children -%}
  {%- if el.tag == 'Sentence' -%}
    {%- for _ in range(indent) -%}
      {{- indent_space -}}
    {%- endfor -%}
    {{- m_text(el) -}}
{#- new #}
{# line -#}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_fig(fig, indent) -%}
{%- for _ in range(indent) -%}
  {{- indent_space -}}
{%- endfor -%}
.. figure:: {{ fig.attr['src'] -}}
{#- new #}
{# line -#}
{%- for el in fig.children -%}
  {{- m_element(el) -}}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_remarks(remarks) -%}
{%- for el in remarks.children -%}
  {%- if el.tag == 'RemarksLabel' -%}
    {{- indent_space -}}{{- m_text(el) -}}
  {%- elif el.tag == 'Sentence' -%}
    {{- margin_space -}}{{- m_text(el) -}}
    {%- if loop.last -%}
{#- new #}
{# line -#}
    {%- endif -%}
  {%- elif el.tag == 'Item' -%}
    {{- m_paragraph_item(el, 0) -}}
  {%- else -%}
    {{- m_element(el) -}}
{#- new #}
{# line -#}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_element(el) -%}
{%- if not el -%}
{%- elif not el.tag -%}
  {{- el -}}
{%- else -%}
  <{{ el.tag }} {%- for key, value in el.attr | dictsort -%}
    {{- ' ' -}}{{- key -}}={{- value -}}
  {%- endfor -%}>
    {%- for child in el.children -%}
      {%- if not child.tag -%}
        {{- child -}}
      {%- else -%}
        {{- m_element(child) -}}
      {%- endif -%}
    {%- endfor -%}
  </{{ el.tag }}>
{%- endif -%}
{%- endmacro -%}



{%- macro m_text(el) -%}
{%- if not el -%}
{%- elif not el.tag -%}
  {{- el -}}
{%- else -%}
  {%- for child in el.children -%}
    {%- if not child.tag -%}
      {{- child -}}
    {%- else -%}
      {{- m_element(child) -}}
    {%- endif -%}
  {%- endfor -%}
{%- endif -%}
{%- endmacro -%}








{{- m_law(law) -}}
