-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from 99ch/highlight-js-integration
Integrate Highlight.js into headers with theme synchronization
- Loading branch information
Showing
8 changed files
with
148 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,46 +14,6 @@ def get_last_statement(code): return ast.unparse(ast.parse(code).body[-1]) | |
from pathlib import Path | ||
|
||
|
||
hjs = ( | ||
# Core highlight.js and copy plugin | ||
Script(src='https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js'), | ||
Script(src='https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/languages/python.min.js'), | ||
Script(src='https://cdn.jsdelivr.net/gh/arronhunt/highlightjs-copy/dist/highlightjs-copy.min.js'), | ||
|
||
# Themes and styles | ||
Link(rel='stylesheet', href='https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/atom-one-dark.css', id='hljs-dark'), | ||
Link(rel='stylesheet', href='https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/atom-one-light.css', id='hljs-light'), | ||
Link(rel='stylesheet', href='https://cdn.jsdelivr.net/gh/arronhunt/highlightjs-copy/dist/highlightjs-copy.min.css'), | ||
|
||
|
||
# Initialization script | ||
Script(''' | ||
// Initialize highlight.js with copy plugin | ||
hljs.addPlugin(new CopyButtonPlugin()); | ||
hljs.configure({ | ||
cssSelector: 'pre code', | ||
languages: ['python'], | ||
ignoreUnescapedHTML: true | ||
}); | ||
// Theme switching logic | ||
function updateTheme() { | ||
const isDark = document.documentElement.classList.contains('dark'); | ||
document.getElementById('hljs-dark').disabled = !isDark; | ||
document.getElementById('hljs-light').disabled = isDark; | ||
} | ||
// Watch for theme changes | ||
new MutationObserver(mutations => | ||
mutations.forEach(m => m.target.tagName === 'HTML' && | ||
m.attributeName === 'class' && updateTheme()) | ||
).observe(document.documentElement, { attributes: true }); | ||
// Initial setup | ||
updateTheme(); | ||
htmx.onLoad(hljs.highlightAll); | ||
''', type='module') | ||
) | ||
def create_flippable_card(content, source_code, extra_cls=None): | ||
"Creates a card that flips between content and source code" | ||
_id = 'f'+str(unqid()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,10 @@ | |
# %% ../nbs/01_core.ipynb | ||
import fasthtml.common as fh | ||
from .foundations import * | ||
from fasthtml.common import is_listy, Div, P, Span, Script, FastHTML, FT, to_xml, show,fast_app | ||
from fasthtml.svg import Svg | ||
from fasthtml.components import Uk_theme_switcher, Main | ||
from fasthtml.common import FastHTML, fast_app | ||
from enum import Enum, auto | ||
from fasthtml.components import Uk_select,Uk_input_tag,Uk_icon | ||
from functools import partial | ||
from itertools import zip_longest | ||
from typing import Union, Tuple, Optional | ||
from fastcore.all import * | ||
import copy, re, httpx | ||
import httpx | ||
from pathlib import Path | ||
|
||
# %% ../nbs/01_core.ipynb | ||
|
@@ -67,7 +61,14 @@ def _headers_theme(color, mode='auto'): | |
'franken_js': "https://unpkg.com/[email protected]/dist/js/core.iife.js", | ||
'icon_js': "https://cdn.jsdelivr.net/gh/answerdotai/monsterui@main/monsterui/icon.iife.js", | ||
'tailwind': "https://cdn.tailwindcss.com", | ||
'daisyui': "https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css"} | ||
'daisyui': "https://cdn.jsdelivr.net/npm/[email protected]/dist/full.min.css", | ||
'highlight_js': "https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/highlight.min.js", | ||
'highlight_python': "https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/languages/python.min.js", | ||
'highlight_light_css': "https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/atom-one-light.css", | ||
'highlight_dark_css': "https://cdn.jsdelivr.net/gh/highlightjs/[email protected]/build/styles/atom-one-dark.css", | ||
'highlight_copy': "https://cdn.jsdelivr.net/gh/arronhunt/highlightjs-copy/dist/highlightjs-copy.min.js", | ||
'highlight_copy_css': "https://cdn.jsdelivr.net/gh/arronhunt/highlightjs-copy/dist/highlightjs-copy.min.css", | ||
} | ||
|
||
def _download_resource(url, static_dir): | ||
"Download a single resource and return its local path" | ||
|
@@ -96,7 +97,7 @@ def _download_resource(url, static_dir): | |
|
||
# %% ../nbs/01_core.ipynb | ||
class Theme(Enum): | ||
"Selector to choose theme and get all headers needed for app. Includes frankenui + tailwind" | ||
"Selector to choose theme and get all headers needed for app. Includes frankenui + tailwind + daisyui + highlight.js options" | ||
def _generate_next_value_(name, start, count, last_values): return name | ||
slate = auto() | ||
stone = auto() | ||
|
@@ -111,25 +112,55 @@ def _generate_next_value_(name, start, count, last_values): return name | |
violet = auto() | ||
zinc = auto() | ||
|
||
|
||
def _create_headers(self, urls, mode='auto', daisy=True): | ||
def _create_headers(self, urls, mode='auto', daisy=True, highlightjs=False): | ||
"Create header elements with given URLs" | ||
hdrs = [ | ||
fh.Link(rel="stylesheet", href=urls['franken_css']), | ||
fh.Script(type="module", src=urls['franken_js']), | ||
fh.Script(type="module", src=urls['icon_js']), | ||
fh.Script(src=urls['tailwind']), | ||
_headers_theme(self.value, mode=mode)] | ||
if daisy: | ||
hdrs += [fh.Link(rel="stylesheet", href=urls['daisyui']),daisy_styles] | ||
|
||
if daisy: | ||
hdrs += [fh.Link(rel="stylesheet", href=urls['daisyui']), daisy_styles] | ||
|
||
if highlightjs: | ||
hdrs += [ | ||
fh.Script(src=urls['highlight_js']), | ||
fh.Script(src=urls['highlight_python']), | ||
fh.Link(rel="stylesheet", href=urls['highlight_light_css'], id='hljs-light'), | ||
fh.Link(rel="stylesheet", href=urls['highlight_dark_css'], id='hljs-dark'), | ||
fh.Script(src=urls['highlight_copy']), | ||
fh.Link(rel="stylesheet", href=urls['highlight_copy_css']), | ||
fh.Script(''' | ||
hljs.addPlugin(new CopyButtonPlugin()); | ||
hljs.configure({ | ||
cssSelector: 'pre code', | ||
languages: ['python'], | ||
ignoreUnescapedHTML: true | ||
}); | ||
function updateTheme() { | ||
const isDark = document.documentElement.classList.contains('dark'); | ||
document.getElementById('hljs-dark').disabled = !isDark; | ||
document.getElementById('hljs-light').disabled = isDark; | ||
} | ||
new MutationObserver(mutations => | ||
mutations.forEach(m => m.target.tagName === 'HTML' && | ||
m.attributeName === 'class' && updateTheme()) | ||
).observe(document.documentElement, { attributes: true }); | ||
updateTheme(); | ||
htmx.onLoad(hljs.highlightAll); | ||
''', type='module'), | ||
] | ||
|
||
return hdrs | ||
|
||
def headers(self, mode='auto', daisy=True): | ||
def headers(self, mode='auto', daisy=True, highlightjs=False): | ||
"Create frankenui and tailwind cdns" | ||
return self._create_headers(HEADER_URLS, mode=mode, daisy=daisy) | ||
return self._create_headers(HEADER_URLS, mode=mode, daisy=daisy, highlightjs=highlightjs) | ||
|
||
def local_headers(self, mode='auto', static_dir='static'): | ||
def local_headers(self, mode='auto', static_dir='static', daisy=True, highlightjs=False): | ||
"Create headers using local files downloaded from CDNs" | ||
Path(static_dir).mkdir(exist_ok=True) | ||
local_urls = dict([_download_resource(url, static_dir) for url in HEADER_URLS.items()]) | ||
return self._create_headers(local_urls, mode=mode,daisy=daisy) | ||
return self._create_headers(local_urls, mode=mode, daisy=daisy, highlightjs=highlightjs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.