refactor: main

This commit is contained in:
thek4n 2026-07-13 19:13:53 +03:00
parent b6cbc9123d
commit b1114217e0
3 changed files with 16 additions and 19 deletions

View File

@ -41,6 +41,6 @@ repos:
- id: check-todos # [NOGREP] - id: check-todos # [NOGREP]
name: check-todos # [NOGREP] name: check-todos # [NOGREP]
language: system language: system
entry: sh -c "! grep --color=always --binary-files=without-match --dereference-recursive --exclude-dir='notes' --exclude-dir='target' --exclude-dir='node_modules' --exclude-dir='.git' --exclude='.pre-commit-config.yaml' --exclude-dir='venv' 'TODO' | grep -v '[NOGREP]'" # [NOGREP] entry: sh -c "! grep --color=always --binary-files=without-match --dereference-recursive --exclude-dir='notes' --exclude-dir='target' --exclude-dir='node_modules' --exclude-dir='.git' --exclude='.pre-commit-config.yaml' --exclude-dir='venv' 'TODO' | grep -v '\[NOGREP\]'" # [NOGREP]
pass_filenames: false pass_filenames: false
always_run: true always_run: true

View File

@ -25,6 +25,7 @@ use tokio_stream::wrappers::BroadcastStream;
use syntect::highlighting::ThemeSet; use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet; use syntect::parsing::SyntaxSet;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
use webbrowser; use webbrowser;
@ -98,8 +99,6 @@ struct Args {
#[derive(Clone)] #[derive(Clone)]
struct AppState { struct AppState {
syntax_set: Arc<SyntaxSet>,
theme_set: Arc<ThemeSet>,
tx: Arc<broadcast::Sender<String>>, tx: Arc<broadcast::Sender<String>>,
root: PathBuf, root: PathBuf,
is_root_file: bool, is_root_file: bool,
@ -123,14 +122,9 @@ async fn main() {
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
let ss = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let (tx, _rx) = broadcast::channel::<String>(100); let (tx, _rx) = broadcast::channel::<String>(100);
let state = AppState { let state = AppState {
syntax_set: Arc::new(ss),
theme_set: Arc::new(ts),
tx: Arc::new(tx), tx: Arc::new(tx),
root: args.root.clone(), root: args.root.clone(),
is_root_file, is_root_file,
@ -179,7 +173,7 @@ fn resolve_addr(host: &str, port: u16) -> io::Result<SocketAddr> {
async fn root_handler(State(state): State<AppState>) -> impl IntoResponse { async fn root_handler(State(state): State<AppState>) -> impl IntoResponse {
if state.is_root_file { if state.is_root_file {
match render_single_file(&state.root, &state).await { match render_single_file(&state.root).await {
Ok(template) => template.into_response(), Ok(template) => template.into_response(),
Err(e) => e.into_response(), Err(e) => e.into_response(),
} }
@ -193,7 +187,6 @@ async fn root_handler(State(state): State<AppState>) -> impl IntoResponse {
async fn render_single_file( async fn render_single_file(
file_path: &StdPath, file_path: &StdPath,
state: &AppState,
) -> Result<NoteTemplate, StatusCode> { ) -> Result<NoteTemplate, StatusCode> {
let metadata = match fs::metadata(file_path).await { let metadata = match fs::metadata(file_path).await {
Ok(m) => m, Ok(m) => m,
@ -217,14 +210,17 @@ async fn render_single_file(
let display_path = ""; let display_path = "";
let ss = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let html_content = if extension == "md" { let html_content = if extension == "md" {
markdown_to_html(&content, &state.syntax_set, &state.theme_set, display_path) markdown_to_html(&content, display_path)
} else { } else {
code_to_html( code_to_html(
&content, &content,
extension.as_str(), extension.as_str(),
&state.syntax_set, &ss,
&state.theme_set, &ts,
) )
}; };
@ -316,14 +312,17 @@ async fn serve_file(
Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR), Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR),
}; };
let ss = SyntaxSet::load_defaults_newlines();
let ts = ThemeSet::load_defaults();
let html_content = if extension == "md" { let html_content = if extension == "md" {
markdown_to_html(&content, &state.syntax_set, &state.theme_set, &full_path) markdown_to_html(&content, &full_path)
} else { } else {
code_to_html( code_to_html(
&content, &content,
extension.as_str(), extension.as_str(),
&state.syntax_set, &ss,
&state.theme_set, &ts,
) )
}; };

View File

@ -2,10 +2,8 @@ use pulldown_cmark::{
BlockQuoteKind, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, BlockQuoteKind, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd,
TextMergeStream, TextMergeStream,
}; };
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;
pub fn markdown_to_html(markdown: &str, ss: &SyntaxSet, ts: &ThemeSet, _file_path: &str) -> String { pub fn markdown_to_html(markdown: &str, _file_path: &str) -> String {
let mut options = Options::empty(); let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES); options.insert(Options::ENABLE_TABLES);
options.insert(Options::ENABLE_FOOTNOTES); options.insert(Options::ENABLE_FOOTNOTES);