Skip to content

Commit

Permalink
[feature] outline
Browse files Browse the repository at this point in the history
  • Loading branch information
WindLX committed Dec 6, 2023
1 parent 9ecfdc5 commit d1b0d2a
Show file tree
Hide file tree
Showing 75 changed files with 1,961 additions and 1,489 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ Pap 需要 Python 3.11.4 的运行环境. 所有脚本均可通过 `Get-Help scr
- [x] File Lock
- [x] Not save tip
- [x] File Rename
- [ ] Note Tag
- [ ] Resource Rightbar
- [x] Note Tag
- [x] Rightbar
- [x] Private Lock
- [ ] HousePage
- [ ] Keymap
- [ ] Style
- [ ] Timeline
- [x] Title List
- [ ] Markdown Ref net
- [ ] Table
- [ ] Database
- [ ] OCR
- [ ] PDF information extraction function
- [ ] More powerful PDF browsing and editing functions
Expand Down
6 changes: 3 additions & 3 deletions data/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ port = 13956

[basic]
title = "Pap"
log_level = "DEBUG"
log_level = "WARNING"

[path]
note_dir = "./data/note/"
log_path = "./data/paps.log"
tag_path = "./data/tags.db"
log_path = "./data/pap.log"
tag_path = "./data/tag.db"
emoji_path = "./data/emoji.db"

[dev]
Expand Down
4 changes: 2 additions & 2 deletions md/md_encoder/src/proto_generator.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use md_parser::expr::*;
use md_parser::generator::Generator;
use md_parser::generator::ParaGenerator;
use md_parser::Result as MResult;
use prost::Message;

Expand Down Expand Up @@ -163,7 +163,7 @@ impl ProtoGenerator {
}
}

impl Generator<proto::Block> for ProtoGenerator {
impl ParaGenerator<proto::Block> for ProtoGenerator {
fn parallel_threshold(&self) -> usize {
self.parallel_threshold
}
Expand Down
30 changes: 30 additions & 0 deletions md/md_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ pub struct Title<'md> {
pub content: Paragraph<'md>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct RawTitle {
pub line: usize,
pub level: TitleLevel,
pub content: String,
}

#[derive(Debug, Clone, PartialEq)]
pub enum TitleLevel {
H1,
Expand Down Expand Up @@ -122,6 +129,29 @@ pub struct Link<'md> {
pub href: Option<&'md str>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct RawLink {
pub content: String,
pub href: Option<String>,
pub is_md: bool,
}

impl<'md> From<Link<'md>> for RawLink {
fn from(value: Link<'md>) -> Self {
RawLink {
content: value.content,
href: match value.href {
Some(href) => Some(href.to_string()),
None => None,
},
is_md: value
.href
.map(|href| href.starts_with("md://"))
.unwrap_or_default(),
}
}
}

#[derive(Debug, Clone, PartialEq)]
pub struct Image<'md> {
pub title: Option<String>,
Expand Down
40 changes: 39 additions & 1 deletion md/md_parser/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
use crate::expr::*;
use crate::Result;

pub trait Generator<T>
where
T: Sized,
{
fn generate(&self, block: &Result<Block>) -> T;
fn process(&self, input: String) -> Vec<T> {
let split_blocks = crate::spliter::split(&input);
let bs: Vec<Result<Block>> = split_blocks
.iter()
.map(|sb| crate::parser::parse(sb))
.collect();
bs.iter().map(|b| self.generate(b)).collect()
}
}

#[cfg(feature = "parallel")]
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};

#[cfg(feature = "parallel")]
pub trait Generator<T>
pub trait ParaGenerator<T>
where
T: Sized + Send,
Self: Sync,
Expand Down Expand Up @@ -46,3 +61,26 @@ where
self.generate(&b)
}
}

pub trait StatusGenerator {
fn get_titles(&self, input: String) -> Vec<RawTitle> {
let split_blocks = crate::spliter::split(&input);
let mut bs = Vec::new();
for sb in split_blocks.iter().enumerate() {
let t = crate::parser::parse_title_only(sb.0, &sb.1);
if let Some(t) = t {
bs.push(t);
}
}
bs
}
fn get_refs(&self, input: String) -> Vec<RawLink> {
let split_blocks = crate::spliter::split(&input);
let mut bs = Vec::new();
for sb in split_blocks {
let l = crate::parser::parse_link_only(&sb);
bs.extend(l)
}
bs
}
}
67 changes: 67 additions & 0 deletions md/md_parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ impl<'md> Parser {
}
}

pub fn parse_title_only(&mut self, line: usize, input: &'md SplitBlock) -> Option<RawTitle> {
match input {
SplitBlock::Paragraph(pa) => match pa.as_bytes().get(0) {
Some(b'#') => match self.parse_raw_title(line, &pa.as_bytes()) {
Ok(t) => Some(t),
Err(_) => None,
},
_ => None,
},
_ => None,
}
}

pub fn parse_link_only(&mut self, input: &'md SplitBlock) -> Vec<RawLink> {
match input {
SplitBlock::Paragraph(pa) => match self.parse_paragraph(pa.as_bytes()) {
Ok(p) => {
let ll =
p.0.iter()
.filter(|s| matches!(s, Sentence::Link(_)))
.map(|s| match s {
Sentence::Link(l) => RawLink::from(l.clone()),
_ => unreachable!(),
})
.collect();
return ll;
}
_ => return Vec::new(),
},
_ => Vec::new(),
}
}

fn parse_title(&mut self, input: &'md [u8]) -> Result<Title<'md>> {
let mut level = 1;
let mut input = &input[1..];
Expand All @@ -91,6 +124,30 @@ impl<'md> Parser {
})
}

fn parse_raw_title(&mut self, line: usize, input: &'md [u8]) -> Result<RawTitle> {
let mut level = 1;
let mut input = &input[1..];
loop {
match input.get(0) {
Some(b'#') => {
if level >= 6 {
break;
}
level += 1;
input = &input[1..];
}
Some(b' ') => input = &input[1..],
_ => break,
}
}
let content = String::from_utf8(input.to_vec()).unwrap();
Ok(RawTitle {
line,
level: TitleLevel::from(level),
content,
})
}

fn parse_list(&mut self, input: &'md [u8]) -> Result<ListItem<'md>> {
let mut input = input;
let mut level = 0;
Expand Down Expand Up @@ -443,6 +500,16 @@ pub fn parse(sb: &SplitBlock) -> Result<Block> {
parser.process(sb)
}

pub fn parse_title_only(line: usize, sb: &SplitBlock) -> Option<RawTitle> {
let mut parser = crate::parser::Parser::new();
parser.parse_title_only(line, sb)
}

pub fn parse_link_only(sb: &SplitBlock) -> Vec<RawLink> {
let mut parser = crate::parser::Parser::new();
parser.parse_link_only(sb)
}

#[cfg(test)]
mod tests {
use super::Parser;
Expand Down
4 changes: 1 addition & 3 deletions md/md_parser/src/spliter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ pub fn split(input: &str) -> Vec<SplitBlock> {
continue;
}
let para = input.slice_to_byte(b'\n');
if para.len() > 0 {
blocks.push(SplitBlock::Paragraph(MdChars::new(para)));
}
blocks.push(SplitBlock::Paragraph(MdChars::new(para)));
if input.is_end() {
break;
}
Expand Down
7 changes: 7 additions & 0 deletions md/md_wasm/src/js_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ pub struct JsTitle {
pub content: JsParagraph,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct JsRawTitle {
pub line: usize,
pub level: JsTitleLevel,
pub content: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum JsTitleLevel {
H1,
Expand Down
35 changes: 34 additions & 1 deletion md/md_wasm/src/js_generator.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::js_expr::*;
use md_parser::expr::*;
use md_parser::generator::SingleGenerator;
use md_parser::generator::{SingleGenerator, StatusGenerator};
use md_parser::Result as MResult;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -156,6 +156,39 @@ impl SingleGenerator<Result<JsBlock, JsError>> for JsGenerator {
}
}

#[wasm_bindgen]
pub struct JsStatusGenerator;

#[wasm_bindgen]
impl JsStatusGenerator {
pub fn new() -> Self {
Self
}

pub fn get_js_titles(&self, input: String) -> Vec<JsValue> {
self.get_titles(input)
.iter()
.map(|t| {
let t = JsRawTitle {
line: t.line,
level: match t.level {
TitleLevel::H1 => JsTitleLevel::H1,
TitleLevel::H2 => JsTitleLevel::H2,
TitleLevel::H3 => JsTitleLevel::H3,
TitleLevel::H4 => JsTitleLevel::H4,
TitleLevel::H5 => JsTitleLevel::H5,
TitleLevel::H6 => JsTitleLevel::H6,
},
content: t.content.clone(),
};
serde_wasm_bindgen::to_value(&t).unwrap()
})
.collect()
}
}

impl StatusGenerator for JsStatusGenerator {}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "pap",
"private": true,
"version": "0.1.4",
"version": "0.1.5",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down Expand Up @@ -31,4 +31,4 @@
"vite-plugin-wasm": "^3.2.2",
"vue-tsc": "^1.8.5"
}
}
}
5 changes: 3 additions & 2 deletions src-python/router/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,13 @@ async def set_config(section: str, value: SystemConfigSchema | BasicConfigSchema
if (not flag):
logger.warning(f"{section} not found")
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail=f"`{section}` 查找失败")
status_code=status.HTTP_404_NOT_FOUND, detail=f"`{section}`设置字段查找失败")


@router.put("/set_pwd", status_code=status.HTTP_202_ACCEPTED, include_in_schema=True)
async def set_pwd(pwd: LoginSchema):
logger.info("PUT /login/set_pwd")
if not authentication_manager.set_pwd(pwd.password):
logger.error(f"password save failed")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="密码文件写入异常")
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="密码保存异常")
3 changes: 2 additions & 1 deletion src-python/router/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ async def login(login: LoginSchema):
logger.info("POST /login")
result = authentication_manager.authenticate(login.password)
if not result:
logger.error("login failed due to incorrect password")
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
detail="Incorrect password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = authentication_manager.create_access_token(
Expand Down
9 changes: 4 additions & 5 deletions src-python/router/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,16 @@ def get_note_by_tags(tags_id: TagSetSchema, db: Session = Depends(get_db)) -> li
Returns:
list[NoteModel]: query result, all notes model
"""
logger.debug(f"GET /note/get_note_tags?tags_id={tags_id}")
logger.debug(f"GET /note/get_note_by_tags")
return note.get_notes_by_tags(db, tags_id)


@router.get("/get_note_by_name", response_model=NoteRelationshipSchema, status_code=status.HTTP_200_OK, include_in_schema=True)
def get_note_by_name(note_name: str, index: int, db: Session = Depends(get_db)) -> NoteModel:
def get_note_by_name(note_name: str, db: Session = Depends(get_db)) -> NoteModel:
"""get note by name
Args:
note_name (str): note name
index (int): index
db (Session, optional): database session. Defaults to Depends(get_db).
Raises:
Expand All @@ -94,8 +93,8 @@ def get_note_by_name(note_name: str, index: int, db: Session = Depends(get_db))
NoteModel: query result, note model
"""
logger.debug(
f"GET /note/get_note_by_name?note_name={note_name}&index={index}")
if (data := note.get_note_by_name(db, note_name, index)):
f"GET /note/get_note_by_name?note_name={note_name}")
if (data := note.get_note_by_name(db, note_name)):
return data
else:
raise HTTPException(
Expand Down
Loading

0 comments on commit d1b0d2a

Please sign in to comment.