refactor: main
This commit is contained in:
parent
b6cbc9123d
commit
b1114217e0
@ -41,6 +41,6 @@ repos:
|
||||
- id: check-todos # [NOGREP]
|
||||
name: check-todos # [NOGREP]
|
||||
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
|
||||
always_run: true
|
||||
|
||||
29
src/main.rs
29
src/main.rs
@ -25,6 +25,7 @@ use tokio_stream::wrappers::BroadcastStream;
|
||||
|
||||
use syntect::highlighting::ThemeSet;
|
||||
use syntect::parsing::SyntaxSet;
|
||||
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
use webbrowser;
|
||||
@ -98,8 +99,6 @@ struct Args {
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
syntax_set: Arc<SyntaxSet>,
|
||||
theme_set: Arc<ThemeSet>,
|
||||
tx: Arc<broadcast::Sender<String>>,
|
||||
root: PathBuf,
|
||||
is_root_file: bool,
|
||||
@ -123,14 +122,9 @@ async fn main() {
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let ss = SyntaxSet::load_defaults_newlines();
|
||||
let ts = ThemeSet::load_defaults();
|
||||
|
||||
let (tx, _rx) = broadcast::channel::<String>(100);
|
||||
|
||||
let state = AppState {
|
||||
syntax_set: Arc::new(ss),
|
||||
theme_set: Arc::new(ts),
|
||||
tx: Arc::new(tx),
|
||||
root: args.root.clone(),
|
||||
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 {
|
||||
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(),
|
||||
Err(e) => e.into_response(),
|
||||
}
|
||||
@ -193,7 +187,6 @@ async fn root_handler(State(state): State<AppState>) -> impl IntoResponse {
|
||||
|
||||
async fn render_single_file(
|
||||
file_path: &StdPath,
|
||||
state: &AppState,
|
||||
) -> Result<NoteTemplate, StatusCode> {
|
||||
let metadata = match fs::metadata(file_path).await {
|
||||
Ok(m) => m,
|
||||
@ -217,14 +210,17 @@ async fn render_single_file(
|
||||
|
||||
let display_path = "";
|
||||
|
||||
let ss = SyntaxSet::load_defaults_newlines();
|
||||
let ts = ThemeSet::load_defaults();
|
||||
|
||||
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 {
|
||||
code_to_html(
|
||||
&content,
|
||||
extension.as_str(),
|
||||
&state.syntax_set,
|
||||
&state.theme_set,
|
||||
&ss,
|
||||
&ts,
|
||||
)
|
||||
};
|
||||
|
||||
@ -316,14 +312,17 @@ async fn serve_file(
|
||||
Err(_) => return Err(StatusCode::INTERNAL_SERVER_ERROR),
|
||||
};
|
||||
|
||||
let ss = SyntaxSet::load_defaults_newlines();
|
||||
let ts = ThemeSet::load_defaults();
|
||||
|
||||
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 {
|
||||
code_to_html(
|
||||
&content,
|
||||
extension.as_str(),
|
||||
&state.syntax_set,
|
||||
&state.theme_set,
|
||||
&ss,
|
||||
&ts,
|
||||
)
|
||||
};
|
||||
|
||||
|
||||
@ -2,10 +2,8 @@ use pulldown_cmark::{
|
||||
BlockQuoteKind, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd,
|
||||
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();
|
||||
options.insert(Options::ENABLE_TABLES);
|
||||
options.insert(Options::ENABLE_FOOTNOTES);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user