npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gulp-html-art

v1.2.0

Published

include HTML files and render templates by art-template

Downloads

12

Readme

本插件用于在 HTML 中引入其他 HTML 片段,同时支持多目录查询和art-template模板渲染。

使用方法举例如下(参见 example):

gulpfile.js:

const gulp = require('gulp');
const htmlArt = require('gulp-html-art');

gulp.task('default', function() {
	return gulp.src('html/component/index.html')
		.pipe(htmlArt({
			paths: ['./html/common'],
			formatData: function(data) {
				 return data
			},
			data: {
				useHeader: false
			},
			beautify: {
				indent_char: ' ',
				indent_with_tabs: false
			}
		}))
		.pipe(gulp.dest('./dist'));
});

html/component/index.html:

<template src="head.html" title="Hello World"></template>
<main>
	<template src="../common/main.html"></template>
</main>
<aside>
	<p>next file: component/index.html</p>
	<template src="phrase.html"></template>
	<p>next file: common/phrase.html</p>
	<template src="../common/phrase.html"></template>
</aside>
<template src="footer.html"></template>

html/common/head.html:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>{{ title }}</title>
</head>
<body>

{{if useHeader}}
	<template src="header.html"></template>
{{/if}}

其他文件略(参见 example 目录下的文件)。

注:模板语法参考art-template模板引擎语法

配置参数如下:

  • paths 字符串或数组,定义跨目录查找的目录路径
  • data 模板渲染的初始数据
  • beautify HTML美化参数,传递给 js-beautify 插件

跨目录查找

当模板中引入的子模板无法在当前目录及相对地址中查找到时,将从配置的 paths 目录中去查找(深遍历子目录)。文件索引的优先级是,当前目录最优,其后依次是 paths 中指定的目录。

模板数据

模板中的数据共三种来源:

  1. 在 Gulp 中配置 data 项作为初始数据

  2. 在模板标签中配置属性,如

    <template
    	useHeader
    	toBool="false"
    	toInt="2"
    	src="header.html"
    	title="Hello World"
    ></template>

    将得到数据如下:

    {
    	useHeader: "useHeader",
    	toBool: false,
    	toInt: 2,
    	src: 'header.html',
    	title: 'hello world'
    }

    属性解析规则如下: ​

    • falsetrue 被视为 Boolean 类型
    • 数字字符串被视为 Number 类型
    • 如果属性无值,则值为 String 类型的属性名
  3. 模板标签的内容,如:

    <template src="header.html">
    {
    	"useHeader": true,
    	"toBool": false
    }
    </template>

    标签内的内容将由 eval() 解析。此外,src 必须写在标签属性中。

    或者,作如下定义:

    <template src="header.html">
    	<fragment id="varname">
    		<a href="#">click here</a>
    	</fragment>
    </template>

    将得到:

    {
    	varname: '<a href="#">click here</a>'
    }

    利用 fragment 可以很方便地传入 HTML 代码(提示:有的模板编译引擎有字符串转义功能,如果发现代码被转义,应当从模板引擎处查找问题。)

    注意,由于本页默认数据先于模板被处理,因此定义本页默认数据标签在模板中有类似于“作用域提升”的效果,即当前页面使用数据渲染的代码可以写在用标签定义数据代码之前

所有数据将层层传递,从初始数据到父模板,再到子模板,优先级是:标签内容数据 > 标签属性 > 继承数据 > 本页默认数据。

HTML美化

内置调用了 js-beautify 插件,对最后的结果进行美化。默认定义了如下参数:

{
	indent_size: 4,
	indent_char: '\t',
	indent_with_tabs: true,
	preserve_newlines: true,
	max_preserve_newlines: 1
}

可通过配置项 beautify 覆盖本插件的默认配置和添加其他可用配置。详情请见 js-beautify 插件。