Skip to content

Commit

Permalink
re add: other selector
Browse files Browse the repository at this point in the history
  • Loading branch information
LincZero committed Feb 19, 2023
1 parent fee5498 commit b1a1579
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 185 deletions.
290 changes: 105 additions & 185 deletions src/manager/abMdBaseSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ function easySelector(
frist_reg:RegExp
):MdSelectorRangeSpecSimp|null{
let mdRange:MdSelectorRangeSpecSimp = {
// 注意区分from_line(从匹配行起记)和mdRange.from_line(从头部选择器起记)
from_line: from_line-1,
to_line: from_line+1,
header: "",
selector: selector,
levelFlag: "",
content: "",
prefix: ""
}
Expand All @@ -26,14 +28,16 @@ function easySelector(
mdRange.prefix = first_line_match[1] // 可以是空
// 验证header
let header_line_match:RegExpMatchArray | null
if (ABReg.reg_emptyline.test(list_text[from_line-1]) && from_line>1){
from_line = from_line-2
if (list_text[from_line-1].indexOf(mdRange.prefix)==0
&& ABReg.reg_emptyline.test(list_text[from_line-1]/*.replace(mdRange.prefix, "")*/)
&& from_line>1
){
mdRange.from_line = from_line-2
}
header_line_match = list_text[mdRange.from_line].match(ABReg.reg_header)
if (!header_line_match
|| !header_line_match[4]
|| header_line_match[1]!=mdRange.prefix
) return null
if (!header_line_match) return null
if (header_line_match[1]!=mdRange.prefix) return null
mdRange.levelFlag = header_line_match[3]
mdRange.header = header_line_match[4]
return mdRange
}
Expand All @@ -49,6 +53,7 @@ function easySelector_headtail(
to_line: from_line+1,
header: "",
selector: selector,
levelFlag: "",
content: "",
prefix: ""
}
Expand All @@ -58,10 +63,14 @@ function easySelector_headtail(
if (!first_line_match) return null
mdRange.prefix = first_line_match[1] // 可以是空
// 验证header
mdRange.levelFlag = first_line_match[3]
mdRange.header = first_line_match[4]
return mdRange
}

/**
* 首尾选择器
*/
const mdSelector_headtail:MdSelectorSpec = {
match: ABReg.reg_headtail,
selector: (list_text, from_line)=>{
Expand All @@ -83,14 +92,17 @@ const mdSelector_headtail:MdSelectorSpec = {
}
mdRange.to_line = last_nonempty+1
mdRange.content = list_text
.slice(mdRange.from_line+1, mdRange.to_line-1)
.slice(from_line+1, mdRange.to_line-1)
.map((line)=>{return line.replace(mdRange.prefix, "")})
.join("\n")
return mdRange
}
}
registerMdSelector(mdSelector_headtail)

/**
* 列表选择器
*/
const mdSelector_list:MdSelectorSpec = {
match: ABReg.reg_list,
selector: (list_text, from_line)=>{
Expand All @@ -114,198 +126,106 @@ const mdSelector_list:MdSelectorSpec = {
}
mdRange.to_line = last_nonempty+1
mdRange.content = list_text
.slice(mdRange.from_line, mdRange.to_line)
.slice(from_line, mdRange.to_line)
.map((line)=>{return line.replace(mdRange.prefix, "")})
.join("\n")
return mdRange
}
}
registerMdSelector(mdSelector_list)

/*
class ABMdSelector_code extends ABMdSelector{
protected blockMatch_keyword(): MdSelectorRangeSpec[]{
const matchInfo: MdSelectorRangeSpec[] = []
const list_text = this.mdText.split("\n")
let prev_from = 0
let prev_header = ""
let code_flag = ""
for (let i=0; i<list_text.length; i++){
if (!code_flag){ // 选择开始标志
// 找开始标志
const match_tmp = list_text[i].match(ABReg.reg_code)
if (!match_tmp) continue
// 尝试找header
if (i!=0) {
const header = list_text[i-1].match(ABReg.reg_header)
if (header){
code_flag = match_tmp[3]
prev_header = header[4]
prev_from = i-1
continue
}
}
// 没有header 不选
if (this.settings.select_code==ConfSelect.ifhead) continue
// 没有header 也选
prev_from = i
code_flag = match_tmp[3]
prev_header = ""
continue
}
else { // 选择结束标志
if (list_text[i].indexOf(code_flag)==-1) continue
const from = this.map_line_ch[prev_from]
const to = this.map_line_ch[i+1]-1 // 包括这一行
matchInfo.push({
from: from,
to: to,
header: prev_header,
selector: "code",
content: prev_header==""?
this.mdText.slice(from, to):
this.mdText.slice(this.map_line_ch[prev_from+1], to)
})
prev_header = ""
code_flag = ""
}
/**
* 代码块选择器
*/
const mdSelector_code:MdSelectorSpec = {
match: ABReg.reg_code,
selector: (list_text, from_line)=>{
let mdRangeTmp = easySelector(list_text, from_line, "code", ABReg.reg_code)
if (!mdRangeTmp) return null
const mdRange = mdRangeTmp
// 开头找到了,现在开始找结束。不需要循环尾处理器
let last_nonempty:number = from_line
for (let i=from_line+1; i<list_text.length; i++){
const line = list_text[i]
// 前缀不符合
if (line.indexOf(mdRange.prefix)!=0) break
const line2 = line.replace(mdRange.prefix, "") // 删掉无用前缀
// 空行
if (ABReg.reg_emptyline.test(line2)) {continue}
last_nonempty = i
// 结束
if (line2.indexOf(mdRange.levelFlag)==0) {last_nonempty = i; break}
}
// 这个不需要尾处理
return matchInfo
mdRange.to_line = last_nonempty+1
mdRange.content = list_text
.slice(from_line, mdRange.to_line)
.map((line)=>{return line.replace(mdRange.prefix, "")})
.join("\n")
return mdRange
}
}
registerMdSelector(mdSelector_code)

class ABMdSelector_quote{
protected blockMatch_keyword(): MdSelectorRangeSpec[]{
const matchInfo: MdSelectorRangeSpec[] = []
const list_text = this.mdText.split("\n")
let prev_from = 0
let prev_header = ""
let is_in_quote = false
for (let i=0; i<list_text.length; i++){
if (!is_in_quote){ // 选择开始标志
if (ABReg.reg_quote.test(list_text[i])){
// 尝试找header
if (i!=0) {
const header = list_text[i-1].match(ABReg.reg_header)
if (header){
prev_header = header[2]
prev_from = i-1
is_in_quote = true
continue
}
}
// 没有header 不选
if (this.settings.select_quote==ConfSelect.ifhead) continue
// 没有header 也选
prev_header = ""
prev_from = i
is_in_quote = true
continue
}
}
else { // 选择结束标志
if (ABReg.reg_quote.test(list_text[i])) continue
const from = this.map_line_ch[prev_from]
const to = this.map_line_ch[i]-1 // 不包括这一行
matchInfo.push({
from: from,
to: to,
header: prev_header,
selector: "quote",
content: prev_header==""?
this.mdText.slice(from, to):
this.mdText.slice(this.map_line_ch[prev_from+1], to)
})
prev_header = ""
is_in_quote = false
}
}
if (is_in_quote){ // 结束循环收尾
const i = list_text.length-1
const from = this.map_line_ch[prev_from]
const to = this.map_line_ch[i+1]-1 // 包括这一行
matchInfo.push({
from: from,
to: to,
header: prev_header,
selector: "quote",
content: prev_header==""?
this.mdText.slice(from, to):
this.mdText.slice(this.map_line_ch[prev_from+1], to)
})
prev_header = ""
is_in_quote = false
/**
* 引用块选择器
*/
const mdSelector_quote:MdSelectorSpec = {
match: ABReg.reg_quote,
selector: (list_text, from_line)=>{
let mdRangeTmp = easySelector(list_text, from_line, "quote", ABReg.reg_quote)
if (!mdRangeTmp) return null
const mdRange = mdRangeTmp
// 开头找到了,现在开始找结束。不需要循环尾处理器
let last_nonempty:number = from_line
for (let i=from_line+1; i<list_text.length; i++){
const line = list_text[i]
// 前缀不符合
if (line.indexOf(mdRange.prefix)!=0) break
const line2 = line.replace(mdRange.prefix, "") // 删掉无用前缀
// 引用
if (ABReg.reg_quote.test(line2)) {last_nonempty = i; continue}
break
}
return matchInfo
mdRange.to_line = last_nonempty+1
mdRange.content = list_text
.slice(from_line, mdRange.to_line)
.map((line)=>{return line.replace(mdRange.prefix, "")})
.join("\n")
return mdRange
}
}
registerMdSelector(mdSelector_quote)

class ABMdSelector_heading{
protected blockMatch_keyword(): MdSelectorRangeSpec[]{
const matchInfo: MdSelectorRangeSpec[] = []
const list_text = this.mdText.split("\n")
let prev_from = 0
let prev_header = ""
let prev_heading_level = 0
for (let i=0; i<list_text.length; i++){
if (prev_heading_level==0){ // 选择开始标志
const match_tmp = list_text[i].match(ABReg.reg_heading)
if (!match_tmp) continue
// 尝试找header
if (i!=0) {
const header = list_text[i-1].match(ABReg.reg_header)
if (header){
prev_heading_level = match_tmp[3].length
prev_header = header[4]
prev_from = i-1
continue
}
}
// 没有header 不选
if (this.settings.select_code==ConfSelect.ifhead) continue
// 没有header 也选
prev_from = i
prev_heading_level = match_tmp[3].length
prev_header = ""
continue
}
else { // 选择结束标志
const match_tmp = list_text[i].match(ABReg.reg_heading)
if (!match_tmp) continue
if (match_tmp[3].length >= prev_heading_level) continue // 【改】可选同级
const from = this.map_line_ch[prev_from]
const to = this.map_line_ch[i]-1 // 不包括这一行
matchInfo.push({
from: from,
to: to,
header: prev_header,
selector: "heading",
content: prev_header==""?
this.mdText.slice(from, to):
this.mdText.slice(this.map_line_ch[prev_from+1], to)
})
// 需要向上回溯一行
prev_header = ""
prev_heading_level = 0
i--
}
}
if(prev_heading_level>0){
const i = list_text.length-1
const from = this.map_line_ch[prev_from]
const to = this.map_line_ch[i+1]-1 // 包括这一行
matchInfo.push({
from: from,
to: to,
header: prev_header,
selector: "heading",
content: prev_header==""?
this.mdText.slice(from, to):
this.mdText.slice(this.map_line_ch[prev_from+1], to)
})
/**
* 标题选择器
*/
const mdSelector_heading:MdSelectorSpec = {
match: ABReg.reg_heading,
selector: (list_text, from_line)=>{
let mdRangeTmp = easySelector(list_text, from_line, "heading", ABReg.reg_heading)
if (!mdRangeTmp) return null
const mdRange = mdRangeTmp
// 开头找到了,现在开始找结束。不需要循环尾处理器
let last_nonempty:number = from_line
for (let i=from_line+1; i<list_text.length; i++){
const line = list_text[i]
// 前缀不符合
if (line.indexOf(mdRange.prefix)!=0) break
const line2 = line.replace(mdRange.prefix, "") // 删掉无用前缀
// 空行
if (ABReg.reg_emptyline.test(line2)) {continue}
last_nonempty = i
// 更大的标题
const match = line2.match(ABReg.reg_heading)
if (!match) continue
if (match[3].length > mdRange.levelFlag.length) {break}
}
return matchInfo
mdRange.to_line = last_nonempty+1
mdRange.content = list_text
.slice(from_line, mdRange.to_line)
.map((line)=>{return line.replace(mdRange.prefix, "")})
.join("\n")
return mdRange
}
}*/
}
registerMdSelector(mdSelector_heading)
1 change: 1 addition & 0 deletions src/manager/abMdSelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export interface MdSelectorRangeSpecSimp {
to_line: number, // .
header: string, // 头部信息
selector: string, // 选择器(范围选择方式)
levelFlag: string,// (用来判断code符号或heading层级的)
content: string, // 内容信息
prefix: string,
}
Expand Down

0 comments on commit b1a1579

Please sign in to comment.