o
    ;j+                     @  s   d Z ddlmZ ddlZddlZddlZddlmZ d&d
dZd'ddZ	d(ddZ
d)ddZd*ddZd+ddZd,d d!Zd-d#d$Zed%krKe  dS dS ).zHRender a small, self-contained Markdown report to browser-friendly HTML.    )annotationsN)Pathtextstrusedset[str]returnc                 C  st   t dd|   }t jdd|t jdd}|sd}|}d}||v r3| d| }|d7 }||v s$|| |S )	Nz	`([^`]+)`z\1z[^\w\u4e00-\u9fff]+-)flagssection      )resubstriplowerUNICODEadd)r   r   slugbasecounter r   %paper/scripts/render_markdown_html.pyslugify   s   
r   c              	   C  s   t d| }g }|D ]>}|dr'|dr'|dt|dd  d q
t|}t ddd	 |}t d
d|}t dd|}|| q
d|S )Nz	(`[^`]*`)`z<code>r   z</code>z\[([^\]]+)\]\(([^)]+)\)c                 S  s.   dt j| ddd dt | d dS )Nz	<a href="r   Tquote">r   </a>)htmlescapegroup)mr   r   r   <lambda>%   s   zparse_inline.<locals>.<lambda>z\*\*([^*]+)\*\*z<strong>\1</strong>z\*([^*]+)\*z<em>\1</em> )	r   split
startswithendswithappendr    r!   r   join)r   partsrenderedpartescapedr   r   r   parse_inline   s     

r/   lineboolc                 C  sH   |   }d|vr
dS dd | ddD }t|o#tdd |D S )N|Fc                 S     g | ]}|  qS r   r   .0cellr   r   r   
<listcomp>5       z&is_table_separator.<locals>.<listcomp>c                 s  s     | ]}t d |p
dV  qdS )z	:?-{3,}:?r%   N)r   	fullmatchr5   r   r   r   	<genexpr>6       z%is_table_separator.<locals>.<genexpr>)r   r&   r1   all)r0   strippedcellsr   r   r   is_table_separator1   s
   r@   	list[str]c                 C  s   dd |    ddD S )Nc                 S  r3   r   r4   r5   r   r   r   r8   :   r9   z#split_table_row.<locals>.<listcomp>r2   )r   r&   )r0   r   r   r   split_table_row9   s   rB   linesc                 C  s   t | d }dd | dd  D }ddd |D }g }|D ](}|dgt|t|   }ddd |d t| D }|d	| d
 qd| dd| dS )Nr   c                 S  s   g | ]}t |qS r   )rB   r6   r0   r   r   r   r8   ?   r9   z render_table.<locals>.<listcomp>r   r%   c                 s       | ]}d t | dV  qdS )z<th>z</th>Nr/   r5   r   r   r   r;   A   r<   zrender_table.<locals>.<genexpr>c                 s  rE   )z<td>z</td>NrF   r5   r   r   r   r;   E   r<   z<tr>z</tr>z*<div class="table-wrap"><table><thead><tr>z</tr></thead><tbody>z</tbody></table></div>)rB   r*   lenr)   )rC   headerrowsthead	body_rowsrowpaddedr?   r   r   r   render_table=   s    rN   markdown&tuple[str, list[tuple[int, str, str]]]c                   s"  |   }g  g }t }g d}d& fdd}|t|k r|| }| }|s0|  |d7 }q|dr|  |dd   }g }	|d7 }|t|k ro||  dso|	||  |d7 }|t|k ro||  drU|t|k ry|d7 }|rd	tj|d
d dnd}
 d|
 dttd	|	 d qt
d|}|r|  t|d}|d }t||}||||f  d| d| d| dt| d| d |d7 }q|dr=|d t|k r=t||d  r=|  || ||d  g}|d7 }|t|k r5||  dr5|||  |d7 }|t|k r5||  ds t| qt
d|}t
d|}|sO|r|  |rWdnd}|r^dnd}g }|t|k rt
|||  }|swn|dt|d  d |d7 }|t|k si d | dd	| d!| d q|dr|  g }|t|k r||  dr|||  dd    |d7 }|t|k r||  dsd"	| } d#t| d$ q| |d7 }|t|k s|  d%	 |fS )'Nr   r   Nonec                    sF   r!d dd D  } | r dt|  d   d S d S )N c                 s  s    | ]}|  V  qd S )Nr4   rD   r   r   r   r;   Z   s    z;render_markdown.<locals>.flush_paragraph.<locals>.<genexpr>z<p>z</p>)r*   r   r)   r/   clear)r   blocks	paragraphr   r   flush_paragraphX   s   z(render_markdown.<locals>.flush_paragraphr   z```   z class="language-Tr   "r%   z
<pre><code>
   z</code></pre>z^(#{1,6})\s+(.+)$r   z<hz id="z"><a class="anchor" href="#z)" aria-label="Link to this section">#</a>z</hr2   z^\d+\.\s+(.+)$z^[-*]\s+(.+)$olulz<li>z</li><z</rR   z<blockquote><p>z</p></blockquote>
r   rQ   )
splitlinessetrG   r   r'   r)   r    r!   chrr*   r   matchr"   r   r/   r@   rN   )rO   rC   toc
used_slugsirW   r0   r>   language
code_lines
class_nameheadingleveltitleanchortable_linesordered	unorderedlist_tagitem_patternitemsitemquote_linesr   r   rT   r   render_markdownP   s   
(
0"" $""
Lrw   rm   bodyre   list[tuple[int, str, str]]c                 C  s2   d dd |D }dt|  d| d| dS )Nr_   c              	   s  s<    | ]\}}}|d krd| d| dt | dV  qdS )rX   z<a class="toc-level-z	" href="#r   r   N)r    r!   )r6   rl   r   rn   r   r   r   r;      s    zrender_page.<locals>.<genexpr>z<!doctype html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>a  </title>
  <style>
    :root {
      color-scheme: light;
      --bg: #f7f8fb;
      --paper: #ffffff;
      --ink: #17202a;
      --muted: #667085;
      --line: #d8dee8;
      --accent: #0f766e;
      --accent-soft: #e6f4f1;
      --code-bg: #101820;
      --code-ink: #f4f7fb;
      --table-head: #eef2f7;
    }
    * { box-sizing: border-box; }
    html { scroll-behavior: smooth; }
    body {
      margin: 0;
      background: var(--bg);
      color: var(--ink);
      font: 17px/1.75 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC",
        "PingFang SC", "Microsoft YaHei", sans-serif;
    }
    .shell {
      display: grid;
      grid-template-columns: minmax(0, 1fr) 280px;
      gap: 32px;
      width: min(1220px, calc(100vw - 40px));
      margin: 36px auto;
      align-items: start;
    }
    article {
      background: var(--paper);
      border: 1px solid var(--line);
      border-radius: 8px;
      padding: 44px min(6vw, 72px);
      box-shadow: 0 18px 45px rgba(15, 23, 42, 0.08);
    }
    nav {
      position: sticky;
      top: 24px;
      max-height: calc(100vh - 48px);
      overflow: auto;
      background: var(--paper);
      border: 1px solid var(--line);
      border-radius: 8px;
      padding: 18px;
      box-shadow: 0 12px 30px rgba(15, 23, 42, 0.06);
    }
    nav strong {
      display: block;
      margin-bottom: 10px;
      color: var(--ink);
      font-size: 14px;
    }
    nav a {
      display: block;
      color: var(--muted);
      text-decoration: none;
      font-size: 13px;
      line-height: 1.45;
      padding: 6px 0;
      border-top: 1px solid transparent;
    }
    nav a:hover { color: var(--accent); }
    .toc-level-2 { padding-left: 10px; }
    .toc-level-3 { padding-left: 22px; }
    h1, h2, h3, h4 {
      color: var(--ink);
      line-height: 1.25;
      letter-spacing: 0;
    }
    h1 {
      margin: 0 0 28px;
      font-size: clamp(2rem, 5vw, 3.2rem);
    }
    h2 {
      margin: 42px 0 16px;
      padding-top: 8px;
      border-top: 1px solid var(--line);
      font-size: 1.72rem;
    }
    h3 {
      margin: 30px 0 12px;
      font-size: 1.24rem;
    }
    p { margin: 14px 0; }
    a { color: var(--accent); }
    .anchor {
      float: left;
      margin-left: -1.05em;
      width: 1em;
      opacity: 0;
      text-decoration: none;
    }
    h1:hover .anchor, h2:hover .anchor, h3:hover .anchor { opacity: 0.55; }
    code {
      border-radius: 5px;
      padding: 0.12em 0.34em;
      background: var(--accent-soft);
      color: #0b5f59;
      font-size: 0.92em;
    }
    pre {
      overflow: auto;
      margin: 18px 0;
      padding: 18px 20px;
      border-radius: 8px;
      background: var(--code-bg);
      color: var(--code-ink);
      line-height: 1.55;
    }
    pre code {
      padding: 0;
      background: transparent;
      color: inherit;
      font-size: 0.95em;
    }
    ol, ul { padding-left: 1.4em; }
    li { margin: 8px 0; }
    blockquote {
      margin: 22px 0;
      padding: 12px 18px;
      border-left: 4px solid var(--accent);
      background: var(--accent-soft);
      border-radius: 0 8px 8px 0;
      color: #16423f;
    }
    .table-wrap {
      overflow-x: auto;
      margin: 20px 0 26px;
      border: 1px solid var(--line);
      border-radius: 8px;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      min-width: 680px;
    }
    th, td {
      border-bottom: 1px solid var(--line);
      padding: 10px 12px;
      text-align: left;
      vertical-align: top;
    }
    th {
      background: var(--table-head);
      font-weight: 700;
      color: var(--ink);
    }
    tr:last-child td { border-bottom: 0; }
    @media (max-width: 980px) {
      .shell {
        display: block;
        width: min(100vw - 24px, 820px);
        margin: 12px auto 28px;
      }
      article { padding: 28px 20px; }
      nav {
        position: static;
        margin-bottom: 14px;
        max-height: 240px;
      }
    }
    @media print {
      body { background: #fff; }
      .shell { display: block; width: auto; margin: 0; }
      nav { display: none; }
      article { border: 0; box-shadow: none; padding: 0; }
    }
  </style>
</head>
<body>
  <main class="shell">
    <article>
uW   
    </article>
    <nav aria-label="Table of contents">
      <strong>目录</strong>
z&
    </nav>
  </main>
</body>
</html>
)r*   r    r!   )rm   rx   re   	toc_itemsr   r   r   render_page   s   
 6  : r{   rQ   c                  C  s   t  } | jdtd | jdtd |  }|jjdd}t|\}}|r+|d d n|jj}|j	j
t|||dd t|j	 d S )Nsource)typeoutputzutf-8)encodingr   r   )argparseArgumentParseradd_argumentr   
parse_argsr|   	read_textrw   stemr~   
write_textr{   print)parserargsrO   rx   re   rm   r   r   r   mainv  s   r   __main__)r   r   r   r   r   r   )r   r   r   r   )r0   r   r   r1   )r0   r   r   rA   )rC   rA   r   r   )rO   r   r   rP   )rm   r   rx   r   re   ry   r   r   r`   )__doc__
__future__r   r   r    r   pathlibr   r   r/   r@   rB   rN   rw   r{   r   __name__r   r   r   r   <module>   s$   






_ 
H
