Molbap HF Staff commited on
Commit
048eb9e
·
1 Parent(s): 3cd87e0

python highlight

Browse files
Files changed (1) hide show
  1. app.py +40 -3
app.py CHANGED
@@ -149,7 +149,21 @@ def render_article(article_path: str, component_inserts: dict[str, callable]):
149
  if index % 2 == 0:
150
  # Render markdown content wrapped in article container
151
  html_content = markdown_to_html(part)
152
- gr.HTML(f'<div class="article">{html_content}</div>')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
153
  else:
154
  # Render interactive component or show error
155
  component_builder = component_inserts.get(part)
@@ -571,10 +585,21 @@ html, body, .gradio-container { background: #fff !important; }
571
  font-size: 0.875rem !important;
572
  }
573
 
574
- /* Python syntax highlighting approximation */
 
 
 
 
 
 
 
 
 
 
 
575
  .article pre code .hljs-keyword,
576
  .article pre code .hljs-built_in {
577
- color: #7c3aed !important; /* Purple for keywords */
578
  font-weight: 600 !important;
579
  }
580
 
@@ -597,6 +622,18 @@ html, body, .gradio-container { background: #fff !important; }
597
  font-weight: 600 !important;
598
  }
599
 
 
 
 
 
 
 
 
 
 
 
 
 
600
  /* Blockquotes, images, rules */
601
  .article blockquote { border-left: 4px solid var(--link-text-color); padding-left: 1rem; margin: 1.25rem 0; color: #334155 !important; font-style: italic; }
602
  .article img { display: block; max-width: 100%; height: auto; margin: 1.25rem auto; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.08); }
 
149
  if index % 2 == 0:
150
  # Render markdown content wrapped in article container
151
  html_content = markdown_to_html(part)
152
+ # Add syntax highlighting script
153
+ highlighted_html = f'''
154
+ <div class="article">{html_content}</div>
155
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
156
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/languages/python.min.js"></script>
157
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github.min.css">
158
+ <script>
159
+ document.addEventListener('DOMContentLoaded', function() {{
160
+ document.querySelectorAll('pre code').forEach((block) => {{
161
+ hljs.highlightElement(block);
162
+ }});
163
+ }});
164
+ </script>
165
+ '''
166
+ gr.HTML(highlighted_html)
167
  else:
168
  # Render interactive component or show error
169
  component_builder = component_inserts.get(part)
 
585
  font-size: 0.875rem !important;
586
  }
587
 
588
+ /* Python syntax highlighting with Highlight.js integration */
589
+ .article pre code {
590
+ background: transparent !important;
591
+ color: #1e293b !important;
592
+ padding: 0 !important;
593
+ border-radius: 0 !important;
594
+ font-weight: 400 !important;
595
+ font-size: inherit !important;
596
+ line-height: inherit !important;
597
+ }
598
+
599
+ /* Highlight.js Python syntax highlighting */
600
  .article pre code .hljs-keyword,
601
  .article pre code .hljs-built_in {
602
+ color: #7c3aed !important; /* Purple for keywords like def, class, import */
603
  font-weight: 600 !important;
604
  }
605
 
 
622
  font-weight: 600 !important;
623
  }
624
 
625
+ .article pre code .hljs-params {
626
+ color: #1e293b !important;
627
+ }
628
+
629
+ .article pre code .hljs-attr {
630
+ color: #0d9488 !important; /* Teal for attributes */
631
+ }
632
+
633
+ .article pre code .hljs-type {
634
+ color: #7c3aed !important; /* Purple for types */
635
+ }
636
+
637
  /* Blockquotes, images, rules */
638
  .article blockquote { border-left: 4px solid var(--link-text-color); padding-left: 1rem; margin: 1.25rem 0; color: #334155 !important; font-style: italic; }
639
  .article img { display: block; max-width: 100%; height: auto; margin: 1.25rem auto; border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.08); }