-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
editor.ts
66 lines (56 loc) · 2.9 KB
/
editor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as vscode from 'vscode';
import { getConfig } from './config';
export type OriginEditor = vscode.TextEditor | "active" | undefined;
export function editorText(originEditor: OriginEditor) : string {
const myEditor = (originEditor === "active") ? vscode.window.activeTextEditor : originEditor;
if (!myEditor) {
return "";
}
const text = myEditor.document.getText();
const cursorOffset = myEditor ? myEditor.document.offsetAt(myEditor.selection.anchor) : 0;
let myHTML = "";
let cursorTaggedHtml = "";
// カーソル位置
if ( text.slice(cursorOffset, cursorOffset + 1) == '\n'){
cursorTaggedHtml = text.slice(0, cursorOffset) + '<span id="cursor"> </span>' + text.slice(cursorOffset);
} else {
cursorTaggedHtml = text.slice(0, cursorOffset) + '<span id="cursor">' + text.slice(cursorOffset, cursorOffset + 1) + '</span>' + text.slice(cursorOffset + 1);
}
const paragraphs = cursorTaggedHtml.split('\n');
//console.log(paragraphs);
paragraphs.forEach(paragraph => {
//console.log(paragraph);
if (paragraph.match(/^\s*$/)) {
myHTML += '<p class="blank">_' + paragraph + '</p>';
} else if( paragraph.match(/^<span id="cursor">$/) || paragraph.match(/^<\/span>$/) ){
myHTML += '<p class="blank">_</p><span id="cursor">';
} else {
myHTML += '<p>' + paragraph + '</p>';
}
});
return markUpHtml(myHTML);
}
export function markUpHtml( myHtml: string ){
let taggedHTML = myHtml;
//configuration 読み込み
const config = getConfig();
const userRegex = config.userRegex;
if (userRegex.length > 0){
userRegex.forEach( function(element){
const thisMatch = new RegExp(element[0], 'gi');
const thisReplace = element[1];
taggedHTML = taggedHTML.replace(thisMatch, thisReplace);
//}
});
}
taggedHTML = taggedHTML.replace(/(?<![0-9])([0-9][0-9])(?![0-9])/g, '<span class="tcy">$1</span>');
taggedHTML = taggedHTML.replace(/<p>[#ここから[11一]文字下げ]<\/p>/g, '<div class="indent-1">');
taggedHTML = taggedHTML.replace(/<p>[#ここから[22二]文字下げ]<\/p>/g, '<div class="indent-2">');
taggedHTML = taggedHTML.replace(/<p>[#ここから[33三]文字下げ]<\/p>/g, '<div class="indent-3">');
taggedHTML = taggedHTML.replace(/<p>[#ここで字下げ終わり]<\/p>/g, '</div>');
taggedHTML = taggedHTML.replace(/<!-- (.+?) -->/g, '<span class="comment"><span class="commentbody">$1</span></span>');
taggedHTML = taggedHTML.replace(/|([^|\n]+?)《([^《]+?)》/g, '<ruby>$1<rt>$2</rt></ruby>');
taggedHTML = taggedHTML.replace(/([一-鿏々-〇]+?)《(.+?)》/g, '<ruby>$1<rt>$2</rt></ruby>');
taggedHTML = taggedHTML.replace(/(.+?)[#「\1」に傍点]/g, '<em class="side-dot">$1</em>');
return taggedHTML;
}