Skip to content

Commit

Permalink
Rename stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
thetarnav committed Mar 3, 2024
1 parent 8e93877 commit e0a9c99
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 226 deletions.
92 changes: 46 additions & 46 deletions mds/mds.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const
/** `DOCUMENT | BLOCKQUOTE` */
ANY_ROOT = 262145

/** @enum {(typeof Token_Type)[keyof typeof Token_Type]} */
export const Token_Type = /** @type {const} */({
/** @enum {(typeof Token)[keyof typeof Token]} */
export const Token = /** @type {const} */({
Document: DOCUMENT,
Blockquote: BLOCKQUOTE,
Paragraph: PARAGRAPH,
Expand All @@ -72,9 +72,9 @@ export const Token_Type = /** @type {const} */({
})

/**
* @param {Token_Type} type
* @param {Token} type
* @returns {string } */
export function token_type_to_string(type) {
export function token_to_string(type) {
switch (type) {
case DOCUMENT: return "Document"
case BLOCKQUOTE: return "Blockquote"
Expand Down Expand Up @@ -105,17 +105,17 @@ export const
SRC = 2,
LANG = 4

/** @enum {(typeof Attr_Type)[keyof typeof Attr_Type]} */
export const Attr_Type = /** @type {const} */({
/** @enum {(typeof Attr)[keyof typeof Attr]} */
export const Attr = /** @type {const} */({
Href: HREF,
Src: SRC,
Lang: LANG,
})

/**
* @param {Attr_Type} type
* @param {Attr} type
* @returns {string } */
export function attr_type_to_html_attr(type) {
export function attr_to_html_attr(type) {
switch (type) {
case HREF: return "href"
case SRC : return "src"
Expand Down Expand Up @@ -174,17 +174,17 @@ export function parser_add_text(p) {
export function parser_end_token(p) {
console.assert(p.len > 0, "No nodes to end")
p.len -= 1
p.renderer.end_node(p.renderer.data)
p.renderer.end_token(p.renderer.data)
}

/**
* @param {Parser } p
* @param {Token_Type} type
* @param {Token} type
* @returns {void } */
export function parser_add_token(p, type) {
p.len += 1
p.types[p.len] = type
p.renderer.add_node(p.renderer.data, type)
p.renderer.add_token(p.renderer.data, type)
}

/**
Expand Down Expand Up @@ -240,8 +240,8 @@ export function parser_write(p, chunk) {
continue
default:
p.len -= 1 // remove the line break
p.renderer.add_node(p.renderer.data, LINE_BREAK)
p.renderer.end_node(p.renderer.data)
p.renderer.add_token(p.renderer.data, LINE_BREAK)
p.renderer.end_token(p.renderer.data)
parser_write(p, char)
continue
}
Expand Down Expand Up @@ -335,8 +335,8 @@ export function parser_write(p, chunk) {
continue
case '\n':
if (p.hr_chars < 3) break
p.renderer.add_node(p.renderer.data, HORIZONTAL_RULE)
p.renderer.end_node(p.renderer.data)
p.renderer.add_token(p.renderer.data, HORIZONTAL_RULE)
p.renderer.end_token(p.renderer.data)
p.pending = ""
p.hr_chars = 0
parser_write(p, char)
Expand All @@ -345,7 +345,7 @@ export function parser_write(p, chunk) {

p.hr_chars = 0
}

p.pending = ""
parser_add_token(p, PARAGRAPH)
/* The whole pending text needs to be reprocessed */
Expand Down Expand Up @@ -450,7 +450,7 @@ export function parser_write(p, chunk) {
case STRONG_AST:
case STRONG_UND: {
/** @type {string } */ let symbol = '*'
/** @type {Token_Type} */ let italic = ITALIC_AST
/** @type {Token} */ let italic = ITALIC_AST
if (in_token === STRONG_UND) {
symbol = '_'
italic = ITALIC_UND
Expand Down Expand Up @@ -479,7 +479,7 @@ export function parser_write(p, chunk) {
case ITALIC_AST:
case ITALIC_UND: {
/** @type {string } */ let symbol = '*'
/** @type {Token_Type} */ let strong = STRONG_AST
/** @type {Token} */ let strong = STRONG_AST
if (in_token === ITALIC_UND) {
symbol = '_'
strong = STRONG_UND
Expand Down Expand Up @@ -619,14 +619,14 @@ export function parser_write(p, chunk) {
case "_":
if (in_token & (NO_NESTING | ANY_UND)) break

/* __Strong__
/* __Strong__
^
*/
if ('_' === char) {
p.pending = pending_with_char
continue
}
/* _Em_
/* _Em_
^
*/
if (' ' !== char && '\n' !== char) {
Expand Down Expand Up @@ -778,20 +778,20 @@ export function parser_write(p, chunk) {
}

/**
* @typedef {import("./t.js").Default_Renderer } Default_Renderer
* @typedef {import("./t.js").Default_Renderer_Add_Node} Default_Renderer_Add_Node
* @typedef {import("./t.js").Default_Renderer_End_Node} Default_Renderer_End_Node
* @typedef {import("./t.js").Default_Renderer_Add_Text} Default_Renderer_Add_Text
* @typedef {import("./t.js").Default_Renderer_Set_Attr} Default_Renderer_Set_Attr
* @typedef {import("./t.js").Default_Renderer } Default_Renderer
* @typedef {import("./t.js").Default_Renderer_Add_Token} Default_Renderer_Add_Token
* @typedef {import("./t.js").Default_Renderer_End_Token} Default_Renderer_End_Token
* @typedef {import("./t.js").Default_Renderer_Add_Text } Default_Renderer_Add_Text
* @typedef {import("./t.js").Default_Renderer_Set_Attr } Default_Renderer_Set_Attr
*/

/**
* @param {HTMLElement } root
* @returns {Default_Renderer} */
export function default_renderer(root) {
return {
add_node: default_add_node,
end_node: default_end_node,
add_token: default_add_token,
end_token: default_end_token,
add_text: default_add_text,
set_attr: default_set_attr,
data : {
Expand All @@ -801,8 +801,8 @@ export function default_renderer(root) {
}
}

/** @type {Default_Renderer_Add_Node} */
export function default_add_node(data, type) {
/** @type {Default_Renderer_Add_Token} */
export function default_add_token(data, type) {
/**@type {HTMLElement}*/ let mount
/**@type {HTMLElement}*/ let slot

Expand Down Expand Up @@ -839,8 +839,8 @@ export function default_add_node(data, type) {
data.nodes[data.index] = slot
}

/** @type {Default_Renderer_End_Node} */
export function default_end_node(data) {
/** @type {Default_Renderer_End_Token} */
export function default_end_token(data) {
data.index -= 1
}

Expand All @@ -851,37 +851,37 @@ export function default_add_text(data, text) {

/** @type {Default_Renderer_Set_Attr} */
export function default_set_attr(data, type, value) {
data.nodes[data.index].setAttribute(attr_type_to_html_attr(type), value)
data.nodes[data.index].setAttribute(attr_to_html_attr(type), value)
}


/**
* @typedef {import("./t.js").Logger_Renderer } Logger_Renderer
* @typedef {import("./t.js").Logger_Renderer_Add_Node} Logger_Renderer_Add_Node
* @typedef {import("./t.js").Logger_Renderer_End_Node} Logger_Renderer_End_Node
* @typedef {import("./t.js").Logger_Renderer_Add_Text} Logger_Renderer_Add_Text
* @typedef {import("./t.js").Logger_Renderer_Set_Attr} Logger_Renderer_Set_Attr
* @typedef {import("./t.js").Logger_Renderer } Logger_Renderer
* @typedef {import("./t.js").Logger_Renderer_Add_Token} Logger_Renderer_Add_Token
* @typedef {import("./t.js").Logger_Renderer_End_Token} Logger_Renderer_End_Token
* @typedef {import("./t.js").Logger_Renderer_Add_Text } Logger_Renderer_Add_Text
* @typedef {import("./t.js").Logger_Renderer_Set_Attr } Logger_Renderer_Set_Attr
*/

/** @returns {Logger_Renderer} */
export function logger_renderer() {
return {
data: undefined,
add_node: logger_add_node,
end_node: logger_end_node,
add_token: logger_add_token,
end_token: logger_end_token,
add_text: logger_add_text,
set_attr: logger_set_attr,
}
}

/** @type {Logger_Renderer_Add_Node} */
export function logger_add_node(data, type) {
console.log("add_node:", token_type_to_string(type))
/** @type {Logger_Renderer_Add_Token} */
export function logger_add_token(data, type) {
console.log("add_token:", token_to_string(type))
}

/** @type {Logger_Renderer_End_Node} */
export function logger_end_node(data) {
console.log("end_node")
/** @type {Logger_Renderer_End_Token} */
export function logger_end_token(data) {
console.log("end_token")
}

/** @type {Logger_Renderer_Add_Text} */
Expand All @@ -891,5 +891,5 @@ export function logger_add_text(data, text) {

/** @type {Logger_Renderer_Set_Attr} */
export function logger_set_attr(data, type, value) {
console.log('set_attr: %s="%s"', attr_type_to_html_attr(type), value)
console.log('set_attr: %s="%s"', attr_to_html_attr(type), value)
}
44 changes: 22 additions & 22 deletions mds/t.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Attr_Type, Token_Type} from './mds.js'
import {Attr, Token} from './mds.js'

export type Parser = {
/** {@link Renderer} interface */
Expand All @@ -8,7 +8,7 @@ export type Parser = {
/** Characters for identifying tokens */
pending : string
/** Current token and it's parents (a slice of a tree) */
types : Token_Type[]
types : Token[]
/** Number of tokens in {@link Parser.types types} without root */
len : number
/** For Code_Fence parsing \
Expand All @@ -24,17 +24,17 @@ export type Parser = {
hr_chars: number
}

export type Renderer_Add_Node<TData> = (data: TData, type: Token_Type) => void
export type Renderer_End_Node<TData> = (data: TData) => void
export type Renderer_Add_Text<TData> = (data: TData, text: string) => void
export type Renderer_Set_Attr<TData> = (data: TData, type: Attr_Type, value: string) => void
export type Renderer_Add_Token<TData> = (data: TData, type: Token) => void
export type Renderer_End_Token<TData> = (data: TData) => void
export type Renderer_Add_Text <TData> = (data: TData, text: string) => void
export type Renderer_Set_Attr <TData> = (data: TData, type: Attr, value: string) => void

export type Renderer<TData> = {
data : TData
add_node: Renderer_Add_Node<TData>
end_node: Renderer_End_Node<TData>
add_text: Renderer_Add_Text<TData>
set_attr: Renderer_Set_Attr<TData>
data : TData
add_token: Renderer_Add_Token<TData>
end_token: Renderer_End_Token<TData>
add_text : Renderer_Add_Text <TData>
set_attr : Renderer_Set_Attr <TData>
}

export type Any_Renderer = Renderer<any>
Expand All @@ -43,15 +43,15 @@ export type Default_Renderer_Data = {
nodes: HTMLElement[]
index: number
}
export type Default_Renderer = Renderer <Default_Renderer_Data>
export type Default_Renderer_Add_Node = Renderer_Add_Node<Default_Renderer_Data>
export type Default_Renderer_End_Node = Renderer_End_Node<Default_Renderer_Data>
export type Default_Renderer_Add_Text = Renderer_Add_Text<Default_Renderer_Data>
export type Default_Renderer_Set_Attr = Renderer_Set_Attr<Default_Renderer_Data>
export type Default_Renderer = Renderer <Default_Renderer_Data>
export type Default_Renderer_Add_Token = Renderer_Add_Token<Default_Renderer_Data>
export type Default_Renderer_End_Token = Renderer_End_Token<Default_Renderer_Data>
export type Default_Renderer_Add_Text = Renderer_Add_Text <Default_Renderer_Data>
export type Default_Renderer_Set_Attr = Renderer_Set_Attr <Default_Renderer_Data>

export type Logger_Renderer_Data = undefined
export type Logger_Renderer = Renderer <Logger_Renderer_Data>
export type Logger_Renderer_Add_Node = Renderer_Add_Node<Logger_Renderer_Data>
export type Logger_Renderer_End_Node = Renderer_End_Node<Logger_Renderer_Data>
export type Logger_Renderer_Add_Text = Renderer_Add_Text<Logger_Renderer_Data>
export type Logger_Renderer_Set_Attr = Renderer_Set_Attr<Logger_Renderer_Data>
export type Logger_Renderer_Data = undefined
export type Logger_Renderer = Renderer <Logger_Renderer_Data>
export type Logger_Renderer_Add_Token = Renderer_Add_Token<Logger_Renderer_Data>
export type Logger_Renderer_End_Token = Renderer_End_Token<Logger_Renderer_Data>
export type Logger_Renderer_Add_Text = Renderer_Add_Text <Logger_Renderer_Data>
export type Logger_Renderer_Set_Attr = Renderer_Set_Attr <Logger_Renderer_Data>
Loading

0 comments on commit e0a9c99

Please sign in to comment.