{%- 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) -}}
  {%- 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) -%}
<div class="law-title">
  {{- m_text(law_title) -}}
</div>
{%- for child in law.children -%}
  {%- if child.tag == 'LawNum' -%}
    <div class="law-num">
      （{{- m_text(child) -}}）
    </div>
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_enact_statement(enact_statement) -%}
<div class="enact-statement">
  {{- m_text(enact_statement) -}}
</div>
{%- endmacro -%}



{%- macro m_toc(toc) -%}
<div class="toc">
{%- for el in toc.children -%}
  {%- if el.tag == 'TOCLabel' -%}
    <div class="toc-label law-anchor"
         data-tag="{{- toc.tag -}}"
         data-name="{{- el.children[0] -}}">
      {{- m_text(el) -}}
    </div>
  {%- endif -%}
{%- endfor -%}
<div class="toc-body">
{%- for el in toc.children -%}
  {%- if el.tag == 'TOCLabel' -%}
  {%- elif el.tag in ['TOCPart', 'TOCChapter', 'TOCSection', 'TOCSupplProvision'] -%}
    {{- m_toc_group(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
</div>
{%- endmacro -%}



{%- macro m_toc_group(toc_group) -%}
<div class="toc-group">
{%- for el in toc_group.children -%}
  {%- if el.tag in ['TOCChapter', 'TOCSection', 'TOCSubsection', 'TOCDivision'] -%}
    {{- m_toc_group(el) -}}
  {%- elif el.tag in [
          'SupplProvisionLabel',
          'PartTitle', 'ChapterTitle', 'SectionTitle',
          'SubsectionTitle', 'DivisionTitle'
      ] -%}
    {{- m_text(el) -}}
  {%- elif el.tag == 'ArticleRange' -%}
    {{- m_text(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
{%- 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'
      ] -%}
    {%- set title_indent = {
            'SupplProvisionLabel': 3,
            'PartTitle': 2, 'ChapterTitle': 3, 'SectionTitle': 4,
            'SubsectionTitle': 5, 'DivisionTitle': 6
        }[el.tag] -%}
      {%- if article_group.tag == 'SupplProvision' -%}
        {%- if 'AmendLawNum' in article_group.attr -%}
          {{- context.set('article_prefix', el.children[0] + article_group.attr['AmendLawNum']) -}}
        {%- else -%}
          {{- context.set('article_prefix', el.children[0]) -}}
        {%- endif -%}
      {%- else -%}
        {{- context.set('article_prefix', "") -}}
      {%- endif -%}
    <div class="article-group-title law-anchor"
         style="margin-left: {{- title_indent -}}em;"
         data-tag="{{- article_group.tag -}}"
         data-name="{{- el.children[0] -}}{{- article_group.attr['AmendLawNum'] -}}">
      {{- m_text(el) -}}
      {%- if 'AmendLawNum' in article_group.attr -%}
        （{{- article_group.attr['AmendLawNum'] -}}）
      {%- endif -%}
      {%- if article_group.attr['Extract'] == 'true' -%}
        {{- '　' -}}抄
      {%- endif -%}
    </div>
  {%- elif el.tag == 'Article' -%}
    {{- m_article(el) -}}
  {%- elif el.tag == 'Paragraph' -%}
    {{- m_paragraph_item(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_article(article) -%}
<div class="article">
{%- for el in article.children -%}
  {%- if el.tag == 'ArticleCaption' -%}
    <div class="article-caption">
      {{- m_text(el) -}}
    </div>
  {%- endif -%}
{%- endfor -%}
<div class="article-body">
{{- context.set('first_paragraph', 1) -}}
{%- for el in article.children -%}
  {%- if el.tag == 'ArticleCaption' -%}
  {%- elif el.tag == 'ArticleTitle' -%}
  {%- elif el.tag == 'Paragraph' -%}
    {{- m_paragraph_item(el, article, context.get('first_paragraph')) -}}
    {{- context.set('first_paragraph', 0) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
</div>
{%- endmacro -%}



{%- macro m_paragraph_item(paragraph_item, article, first_paragraph=0) -%}
<div class="paragraph-item">
{%- for el in paragraph_item.children -%}
  {%- if el.tag == 'ParagraphCaption' -%}
    <div class="paragraph-item-caption">
      {{- m_text(el) -}}
    </div>
  {%- endif -%}
{%- endfor -%}



{{- context.set('no_title', 1) -}}

{%- for el in paragraph_item.children -%}
  {%- if el.tag in [
          'ParagraphNum', 'ItemTitle',
          'Subitem1Title', 'Subitem2Title', 'Subitem3Title', 'Subitem4Title',
          'Subitem5Title', 'Subitem6Title', 'Subitem7Title', 'Subitem8Title',
          'Subitem9Title', 'Subitem10Title'
      ] -%}

    {%- if article and paragraph_item.tag == 'Paragraph' and first_paragraph -%}
      {%- for el2 in article.children -%}
        {%- if el2.tag == 'ArticleTitle' -%}
          {%- if el2.children | length -%}
            {{- context.set('no_title', 0) -}}
          {%- endif %}
        {%- endif -%}
      {%- endfor -%}
    {%- endif -%}

    {%- if el.children | length -%}
      {{- context.set('no_title', 0) -}}
    {%- endif %}

  {%- endif %}
{%- endfor %}

<div class="
{%- if context.get('no_title') -%}
indent-first-line
{%- else -%}
paragraph-item-body
{%- endif -%}">
{%- for el in paragraph_item.children -%}
  {%- if el.tag == 'ParagraphCaption' -%}
  {%- elif el.tag in [
          'ParagraphNum', 'ItemTitle',
          'Subitem1Title', 'Subitem2Title', 'Subitem3Title', 'Subitem4Title',
          'Subitem5Title', 'Subitem6Title', 'Subitem7Title', 'Subitem8Title',
          'Subitem9Title', 'Subitem10Title'
      ] -%}
    {%- if article and paragraph_item.tag == 'Paragraph' and first_paragraph -%}
      {%- for el2 in article.children -%}
        {%- if el2.tag == 'ArticleTitle' -%}
          <span class="article-title law-anchor"
                data-tag="{{- article.tag -}}"
                data-name="{{- context.get('article_prefix') -}}{{- el2.children[0] -}}">
            {{- m_text(el2) -}}
          </span>
        {%- endif -%}
      {%- endfor -%}
    {%- endif -%}
    {%- if el.children -%}
      <span class="paragraph-item-num">
        {{- m_text(el) -}}
      </span>
    {%- endif -%}
    {%- if not context.get('no_title') -%}
      {{- '　' -}}
    {%- endif -%}
  {%- elif el.tag in [
          'ParagraphSentence', 'ItemSentence',
          'Subitem1Sentence', 'Subitem2Sentence', 'Subitem3Sentence', 'Subitem4Sentence',
          'Subitem5Sentence', 'Subitem6Sentence', 'Subitem7Sentence', 'Subitem8Sentence',
          'Subitem9Sentence', 'Subitem10Sentence'
      ] -%}
    {{- m_paragraph_item_sentence(el) -}}
  {%- elif el.tag in [
          'Item',
          'Subitem1', 'Subitem2', 'Subitem3', 'Subitem4',
          'Subitem5', 'Subitem6', 'Subitem7', 'Subitem8',
          'Subitem9', 'Subitem10'
      ] -%}
    {{- m_paragraph_item(el) -}}
  {%- elif el.tag == 'TableStruct' -%}
    {{- m_table_struct(el) -}}
  {%- elif el.tag == 'List' -%}
    {{- m_list(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
</div>
{%- endmacro -%}



{%- macro m_paragraph_item_sentence(paragraph_item_sentence) -%}
{%- 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 -%}
      {{- '　' -}}
    {%- endif -%}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- 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) -%}
{%- for el in table_struct.children -%}
  {%- if el.tag == 'Table' -%}
    {{- m_table(el) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



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



{%- macro m_table_row(table_row) -%}
<tr>
{%- for el in table_row.children -%}
  {%- if el.tag == 'TableColumn' -%}
    {{- m_table_column(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</tr>
{%- endmacro -%}



{%- macro m_table_column(table_column) -%}
<td {# -#}
  {%- if 'rowspan' in table_column.attr -%}
    rowspan="{{- table_column.attr['rowspan'] -}}"
  {%- endif -%}
  {%- if 'colspan' in table_column.attr -%}
    colspan="{{- table_column.attr['colspan'] -}}"
  {%- endif -%}
  {# #} style="
    {%- if 'BorderTop' in table_column.attr -%}
      border-top-style: {{- table_column.attr['BorderTop'] -}};
    {%- endif -%}
    {%- if 'BorderBottom' in table_column.attr -%}
      border-bottom-style: {{- table_column.attr['BorderBottom'] -}};
    {%- endif -%}
    {%- if 'BorderLeft' in table_column.attr -%}
      border-left-style: {{- table_column.attr['BorderLeft'] -}};
    {%- endif -%}
    {%- if 'BorderRight' in table_column.attr -%}
      border-right-style: {{- table_column.attr['BorderRight'] -}};
    {%- endif -%}
    {%- if 'Align' in table_column.attr -%}
      text-align: {{- table_column.attr['Align'] -}};
    {%- endif -%}
    {%- if 'Valign' in table_column.attr -%}
      vertical-align: {{- table_column.attr['Valign'] -}};
    {%- endif -%}
  ">
{%- for el in table_column.children -%}
  {%- if el.tag == 'Sentence' -%}
    <div class="sentence">
      {{- m_text(el) -}}
    </div>
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</td>
{%- endmacro -%}



{%- macro m_style_struct(style_struct) -%}
{%- for el in style_struct.children -%}
  {%- if el.tag == 'StyleStructTitle' -%}
    <div class="style_struct_title">
      {{- m_text(el) -}}
    </div>
  {%- elif el.tag == 'Style' -%}
    {{- m_style(el) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
{%- endmacro -%}



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



{%- macro m_appdx_table(appdx_table) -%}
<div class="appdx_table">
{%- for el in appdx_table.children -%}
  {%- if el.tag == 'AppdxTableTitle' -%}
    <div class="appdx_table_title law-anchor"
         data-tag="{{- appdx_table.tag -}}"
         data-name="{{- el.children[0] -}}">
      {{- m_text(el) -}}
      {%- for el2 in appdx_table.children -%}
        {%- if el2.tag == 'RelatedArticleNum' -%}
          {{- m_text(el2) -}}
        {%- endif -%}
      {%- endfor -%}
    </div>
  {%- endif -%}
{%- endfor -%}
<div class="appdx_table_body">
{%- for el in appdx_table.children -%}
  {%- if el.tag == 'AppdxTableTitle' -%}
  {%- elif el.tag == 'RelatedArticleNum' -%}
  {%- elif el.tag == 'TableStruct' -%}
    {{- m_table_struct(el) -}}
  {%- elif el.tag == 'Item' -%}
    {{- m_paragraph_item(el) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
</div>
{%- endmacro -%}



{%- macro m_appdx_style(appdx_style) -%}
<div class="appdx_style">
{%- for el in appdx_style.children -%}
  {%- if el.tag == 'AppdxStyleTitle' -%}
    <div class="appdx_style_title law-anchor"
        data-tag="{{- appdx_style.tag -}}"
        data-name="{{- el.children[0] -}}">
      {{- m_text(el) -}}
      {%- for el2 in appdx_style.children -%}
        {%- if el2.tag == 'RelatedArticleNum' -%}
          {{- m_text(el2) -}}
        {%- endif -%}
      {%- endfor -%}
    </div>
  {%- endif -%}
{%- endfor -%}
<div class="appdx_style_body">
{%- for el in appdx_style.children -%}
  {%- if el.tag == 'AppdxStyleTitle' -%}
  {%- elif el.tag == 'RelatedArticleNum' -%}
  {%- elif el.tag == 'StyleStruct' -%}
    {{- m_style_struct(el) -}}
  {%- elif el.tag == 'Remarks' -%}
    {{- m_remarks(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
</div>
{%- endmacro -%}



{%- macro m_list(list) -%}
<div class="list">
{%- for el in list.children -%}
  {%- if el.tag == 'ListSentence' -%}
    {{- m_list_sentence(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
{%- endmacro -%}



{%- macro m_list_sentence(list_sentence) -%}
<div class="list-sentence">
{%- for el in list_sentence.children -%}
  {%- if el.tag == 'Sentence' -%}
    <div class="sentence">
      {{- m_text(el) -}}
    </div>
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
{%- endmacro -%}



{%- macro m_fig(fig) -%}
[{{ fig.attr['src'] | escape -}}]
{%- for el in fig.children -%}
  {{- m_element(el) -}}
{%- endfor -%}
{%- endmacro -%}



{%- macro m_remarks(remarks) -%}
<div class="remarks">
{%- for el in remarks.children -%}
  {%- if el.tag == 'RemarksLabel' -%}
    {{- m_text(el) -}}
  {%- elif el.tag == 'Sentence' -%}
    {{- '　' -}}{{- m_text(el) -}}
    {%- if loop.last -%}
    {%- endif -%}
  {%- elif el.tag == 'Item' -%}
    {{- m_paragraph_item(el) -}}
  {%- else -%}
    {{- m_element(el) -}}
  {%- endif -%}
{%- endfor -%}
</div>
{%- endmacro -%}



{%- macro m_element(el) -%}
<pre>
{{- ('' + m_element_raw(el)) | escape -}}
</pre>
{%- endmacro -%}



{%- macro m_element_raw(el) -%}
{%- if not el -%}
{%- elif not el.tag -%}
  {{- el | escape -}}
{%- 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_raw(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 -}}
    {%- elif child.tag == 'Ruby' -%}
      {{- m_element_raw(child) -}}
    {%- else -%}
      {{- m_element(child) -}}
    {%- endif -%}
  {%- endfor -%}
{%- endif -%}
{%- endmacro -%}








<div class="law">
{{- m_law(law) -}}
</div>

