国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vscode插件liveserver增加對thymeleaf模板的簡單支持

這篇具有很好參考價值的文章主要介紹了vscode插件liveserver增加對thymeleaf模板的簡單支持。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

vscode插件liveserver增加對thymeleaf模板的簡單支持

背景

vscode+liveserver開發(fā)時,多個頁面引用的公用靜態(tài)資源在每個頁面都寫一個遍比較麻煩,想讓liveserver支持簡單的thymeleaf語法,只要能把公用資源抽出來單獨放到一個文件中聲明即可。

網(wǎng)上找了一下,沒有現(xiàn)成的功能,為方便自己使用,修改了一個liveserver插件。

其它人也可能會遇到同樣的需求,這里把代碼貼出來

實用方式

只有兩個簡單的js文件,同時簡單修改一下liveserver插件的index.js文件即可

liveserver插件的位置:
C:\Users\*\.vscode\extensions\ritwickdey.liveserver-5.7.9\node_modules\live-server(具體路徑,由于機器配置以及使用的liveserver版不同,可能不一樣)

  1. thymeleaf-handler.jsthymeleaf-parser.js拷到插件目錄中的live-server目錄中
  2. 修改index.js文件

liveserver目錄結(jié)構(gòu):

vscode插件liveserver增加對thymeleaf模板的簡單支持

項目結(jié)構(gòu)

include.html

vscode插件liveserver增加對thymeleaf模板的簡單支持

  • env.txt 用于放置環(huán)境變里,在頁面中可使用th:text來引用
  • include.html 用于放置公共引用,僅支持th:fragment

index.html

vscode插件liveserver增加對thymeleaf模板的簡單支持

env.txt

vscode插件liveserver增加對thymeleaf模板的簡單支持

輸出到瀏覽器的結(jié)果

vscode插件liveserver增加對thymeleaf模板的簡單支持

源碼

//index.js

var fs = require('fs'),
	connect = require('connect'),
	serveIndex = require('serve-index'),
	logger = require('morgan'),
	WebSocket = require('faye-websocket'),
	path = require('path'),
	url = require('url'),
	http = require('http'),
	send = require('send'),
	open = require('opn'),
	es = require("event-stream"),
	os = require('os'),
	chokidar = require('chokidar'),
	httpProxy = require('http-proxy'),
	HandlerStream = require('./thymeleaf-handler');//這里增加一行對thymeleaf-handler的引用


    ......


    //在后面找到inject函數(shù)
    function inject(stream) {
			if (injectTag) {
				// We need to modify the length given to browser
				var len = GET_INJECTED_CODE().length + res.getHeader('Content-Length');
				res.setHeader('Content-Length', len);
				var originalPipe = stream.pipe;
				stream.pipe = function (resp) {
					originalPipe.call(stream, es.replace(new RegExp(injectTag, "i"), GET_INJECTED_CODE() + injectTag))
					.pipe(new HandlerStream(root,res)).pipe(resp);// 修改這一句,把原來的 .pipe(resp) 修改為 .pipe(new HandlerStream(root,res)).pipe(resp);
				};
			}
		}

以下為thymeleaf-handler.jsthymeleaf-parser.js的源碼文章來源地址http://www.zghlxwxcb.cn/news/detail-849058.html

//thymeleaf-handler

const fs = require('fs');
const path = require('path')
const stream = require('stream');
let thymeleafParser = require("./thymeleaf-parser");


class HandlerStream extends stream.Transform{
    constructor (root,res) {
        super();     
        this.root = root;   
        this.body = "";
        this.res = res;
        this.byteLen = 0;
    }

    _parse(){
        return thymeleafParser(this.root,this.body);
    }

    _transform (chunk, encoding, callback){
        this.body += chunk;
        this.byteLen += chunk.byteLength;
        callback()
    }

    _getBufferLen(v){
        const buf = Buffer.from(v, 'utf8');                
        return buf.length;  
    }

    _flush (callback){        
        let newBoday = this._parse();        
        let newLen = this._getBufferLen(newBoday);
        var len = (newLen - this.byteLen) + this.res.getHeader('Content-Length');
        this.res.setHeader('Content-Length', len);        
        this.push(newBoday);
        callback();
    }
}




module.exports = HandlerStream;

//thymeleaf-parser
const fs = require('fs');
const path = require('path');


class Fragment{
    constructor(name,content){
        this.paramName = null;
        this.name = null;

        this.decodeName(name);

        //替換對上下文件的引用
        this.content = content.replace("[[@{/}]]","/");

        let parser = new Parser(this.content);

        parser.parseSrc()
        .parseHref();

        this.content = parser.value;
    }

    decodeName(name){
        let r = /"?(.+?)\((.+)\)"?/;
        let m = r.exec(name);
        if(m){
            this.name = m[1];
            this.paramName = m[2];
        }
        else if(/"(.+)"/.test(name)){        
            this.name = name.slice(1,-1);
        }
        else
            this.name = name;
    }


    getContent(param){
        if(param && this.paramName){
            return this.content.replace(`\${${this.paramName}}`,param)
        }        
        else
            return this.content;

    }

}

class Parser{
    constructor(value){
        this.value = value;
    }
    parseSrc(){
        this.value = this.value.replace(/th:src="@\{(.+?)\}"/g,'src="$1"');

        return this;
    }

    parseHref(){
        this.value = this.value.replace(/th:href="@\{(.+?)\}"/g,'href="$1"');

        return this;
    }

    parseText(env){
        let reg = /<(div|a|input|span|button|p|title)(.*?) th:text="(.+?)"(.*?)><\/\1>/g;

        let textBlocks = [];

        let m = reg.exec(this.value);
        while(m){
            m[0];
            m[2];
            textBlocks.push({
                tagContent: m[0],
                tagName: m[1],
                attrs:[m[2],m[4]],
                value: m[3]
            });

            m = reg.exec(this.value);
        }


        reg = /\$\{(.+)\}/;

        for(let b of textBlocks){
            m = reg.exec(b.value);
            if(m && env.getValue(m[1])){
                b.value = env.getValue(m[1]);
            }
            
            let tag = `<${b.tagName}${b.attrs[0]}${b.attrs[1]}>${b.value}</${b.tagName}>`;
            this.value = this.value.replace(b.tagContent,tag);
           
        }

        return this;
    }

    parseBlock(fragments){
     
        function removeBrackets(v){
            if(!v) return v;
            
            let m = /\('(.+)'\)/.exec(v);
            if(m){
                return m[1];
            }
            else{
                return "";
            }
        }
        //<th:block th:include="include :: header('示例')"/>
        let reg = /<th:block th:include="include\s*::\s*([a-zA-z0-9_]+?)(\(.+\))?"\s*\/>/g;


        let blocks = [];

        let m = reg.exec(this.value);
        while(m){
        
            blocks.push({
                tag: m[0],
                name: m[1],
                param: removeBrackets(m[2])
            });
            m = reg.exec(this.value);
        }


        for(let block of blocks){
            let fragment = fragments[block.name];
            if(fragment){
                this.value = this.value.replace(block.tag,fragment.getContent(block.param));
            }
         
        }
        return this;
    }
}

class Evn{
    constructor(){
        this.values = {};
    }

    load(root){        
        let envString = readFile(path.resolve(root,"env.txt"))

        let lines = envString.split('\n');

        let r = /(.+?)=(.+)/;

        for(let l of lines){
            l = l.trim();
            if(l.startsWith("#") || !l) continue;

            let m = r.exec(l);
            if(m){
                this.values[m[1].trim()] = m[2].trim();
            }               
        }
    }

    getValue(key){
        if(this.values[key]){
            return this.values[key];
        }
        else
            return null;
    }

}

function parseTemplate(template){{
    let fragmentReg = /<(div|head|section)\s+th:fragment=(.*?)>(.*?)<\/\1>/gs;

    let fragments = {};

    let m = fragmentReg.exec(template);

    while(m){
        let fragment = new Fragment(m[2],m[3]);
        fragments[fragment.name] = fragment;
     
        m = fragmentReg.exec(template);
    }

    return fragments;
}}

function readFile(fileName){
    if(fs.existsSync(fileName)){
        return fs.readFileSync(fileName).toString("utf8");
    }
    else
        return "";
}

function readTemplate(root){    
    return readFile(path.resolve(root,"include.html"));
}

function parse(root,html){
    
    let fragments = parseTemplate(readTemplate(root));

    let env = new Evn();
    env.load(root);


    let parser = new Parser(html);

    parser.parseSrc()
    .parseHref()
    .parseBlock(fragments)
    .parseText(env);


    return parser.value;
}



module.exports = parse

到了這里,關(guān)于vscode插件liveserver增加對thymeleaf模板的簡單支持的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【Spring Boot】Thymeleaf模板引擎 — Thymeleaf入門

    主要介紹什么是Thymeleaf以及Spring Boot如何集成使用Thymeleaf模板,最后介紹Spring Boot支持的Thymeleaf的一些常用的配置參數(shù)。 Thymeleaf是一款非常優(yōu)秀的服務(wù)器端頁面模板引擎,適用于Web和獨立環(huán)境,具有豐富的標簽語言和函數(shù),能夠處理HTML、XML、JavaScript甚至文本。 Thymeleaf相較于

    2024年02月05日
    瀏覽(24)
  • 【工具】Vscode翻譯插件推薦(不用谷歌翻譯api、支持短句英漢互譯、支持查詞、支持自動補全、不需要瀏覽器)

    【工具】Vscode翻譯插件推薦(不用谷歌翻譯api、支持短句英漢互譯、支持查詞、支持自動補全、不需要瀏覽器)

    需求: 1)偶爾需要查英文生詞; 2)有時候想不起來中文對應(yīng)的英文; 3)不想回到瀏覽器打開一堆網(wǎng)頁; 4)谷歌翻譯掛了。 偶爾需要的需求: 1)短句翻譯。 因為谷歌翻譯掛了,首先,排除最熱門的翻譯插件Vscode Google Translate,以及一系列衍生產(chǎn)品。 由于搜索“translate”

    2023年04月08日
    瀏覽(29)
  • SpringBoot Thymeleaf模板引擎

    SpringBoot Thymeleaf模板引擎

    前端交給我們的頁面,是 html 頁面。如果是我們以前開發(fā),我們需要把他們轉(zhuǎn)成jsp頁面,jsp好處就是當我們查出一些數(shù)據(jù)轉(zhuǎn)發(fā)到JSP頁面以后,我們可以用jsp輕松實現(xiàn)數(shù)據(jù)的顯示,及交互等。 jsp支持非常強大的功能,包括能寫Java代碼,但是呢,我們現(xiàn)在的這種情況,SpringBoot這

    2024年02月13日
    瀏覽(56)
  • thymeleaf模板引擎

    thymeleaf模板引擎

    ThymeleafProperties 配置類 1.默認編碼 2.前綴 3.后綴 相當于視圖解析器? ? 這是學(xué)SpringBoot的必經(jīng)之路,非常重要?。。。ǔ悄闶菍W(xué)前端的) ? 只改了前端代碼點一下這個就可以刷新? ? 傳值過來了? th:text=\\\"${msg}\\\"爆紅,但是可以顯示,F(xiàn)ile-Settings-Editor-Inspection ?取消“Expression

    2024年02月14日
    瀏覽(20)
  • 【SpringBoot】| Thymeleaf 模板引擎

    【SpringBoot】| Thymeleaf 模板引擎

    目錄 Thymeleaf 模板引擎 1. 第一個例子 2. 表達式 ①標準變量表達式 ②選擇變量表達式(星號變量表達式) ③鏈接表達式(URL表達式) 3. Thymeleaf的屬性 ①th:action ②th:method ③th:href ④th:src ⑤th:text ⑥th:style ⑦th:each (重點) ⑧條件判斷 if-unless ⑨switch-case 判斷語句 ⑩th:inline內(nèi)聯(lián)

    2024年02月08日
    瀏覽(21)
  • Thymeleaf SSTI模板注入分析

    Thymeleaf SSTI模板注入分析

    先搭建一個SpringMVC項目,參考這篇文章,或者參考我以前的spring內(nèi)存馬分析那篇文章 https://blog.csdn.net/weixin_65287123/article/details/136648903 簡單寫個servlet 這樣就是訪問到index.jsp 路由解析流程主要就是 Model 和 View 以及最后 Render 。return處打個斷點,看怎么處理的 先進入 invokeAndHa

    2024年04月12日
    瀏覽(24)
  • SpringBoot整合模板引擎Thymeleaf(4)

    SpringBoot整合模板引擎Thymeleaf(4)

    本文原創(chuàng)作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 在之前的教程中,我們介紹了Thymeleaf的基礎(chǔ)知識。在此,以案例形式詳細介紹Thymeleaf的基本使用。 要點概述: 1、在static下創(chuàng)建css文件夾用于存放css文件 2、在static下創(chuàng)建img文件夾用于存放圖片文件 請在pom.xml文

    2024年02月10日
    瀏覽(19)
  • 前端模板引擎Thymeleaf的整合和使用

    目錄 一、添加依賴 1.1首先,在項目的構(gòu)建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依賴。例如,對于 Maven 項目,在 pom.xml 文件中添加以下依賴 1.2保存并更新項目依賴 二、配置Thymeleaf 2.1模板位置配置 2.2模板緩存配置 2.3自定義標簽配置 三、創(chuàng)建模板文件 3.1創(chuàng)建一個HTML文

    2024年04月27日
    瀏覽(32)
  • 【SpringBoot學(xué)習筆記】04. Thymeleaf模板引擎

    【SpringBoot學(xué)習筆記】04. Thymeleaf模板引擎

    ?所有的html元素都可以被thymeleaf替換接管? th:元素名 templates下的只能通過Controller來跳轉(zhuǎn),templates前后端分離,需要模板引擎thymeleaf支持 ?? 模板引擎的作用就是我們來寫一個頁面模板,比如有些值呢,是動態(tài)的,我們寫一些表達式。而這些值,從哪來呢,就是我們在后臺封

    2024年02月13日
    瀏覽(21)
  • SpringBoot自帶模板引擎Thymeleaf使用詳解①

    SpringBoot自帶模板引擎Thymeleaf使用詳解①

    目錄 前言 一、SpringBoot靜態(tài)資源相關(guān)目錄 二、變量輸出 2.1 在templates目錄下創(chuàng)建視圖index.html 2.2 創(chuàng)建對應(yīng)的Controller 2.3 在視圖展示model中的值 三、操作字符串和時間 3.1 操作字符串 3.2 操作時間 ????????Thymeleaf是一款用于渲染XML/HTML5內(nèi)容的模板引擎,類似JSP。它可以輕易的

    2024年02月08日
    瀏覽(20)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包