Browse Source

added optiobal max length for variable values (fix #349)

Former-commit-id: 362069cf22639863960eb8e20e5ee502689c266c
Gildas 6 years ago
parent
commit
fe387ff00a
2 changed files with 27 additions and 7 deletions
  1. 5 1
      extension/ui/pages/help.html
  2. 22 6
      lib/single-file/single-file-core.js

+ 5 - 1
extension/ui/pages/help.html

@@ -237,7 +237,7 @@
 							without altering the document. It may also increase the CPU consumption and the time needed
 							without altering the document. It may also increase the CPU consumption and the time needed
 							to save a page.</p>
 							to save a page.</p>
 						<p class="notice">It is recommended to <u>uncheck</u> this option</p>
 						<p class="notice">It is recommended to <u>uncheck</u> this option</p>
-					</li>					
+					</li>
 				</ul>
 				</ul>
 				<p>Images</p>
 				<p>Images</p>
 				<ul>
 				<ul>
@@ -448,6 +448,10 @@
 			<li><a id="template-variables">Template variables</a>
 			<li><a id="template-variables">Template variables</a>
 				<p>The template variables are used to customize the infobar content or the file name of a saved page.
 				<p>The template variables are used to customize the infobar content or the file name of a saved page.
 					They help to insert dynamic values like the save date or the page title.</p>
 					They help to insert dynamic values like the save date or the page title.</p>
+				<p>
+					You can limit the length of a dynamic value by adding <code>[<em>maxByteSize</em>]</code> just after
+					the variable name. The <code>maxByteSize</code> value is the maximum length of the value in bytes
+					(e.g. `{page-title}[20]` to limit the title to 20 bytes).</p>
 				<ul>
 				<ul>
 					<li><code>{page-title}</code>: the title of the page</li>
 					<li><code>{page-title}</code>: the title of the page</li>
 					<li><code>{page-language}</code>: the language of the page</li>
 					<li><code>{page-language}</code>: the language of the page</li>

+ 22 - 6
lib/single-file/single-file-core.js

@@ -1901,13 +1901,29 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 		}
 		}
 
 
 		static async evalTemplateVariable(template, variableName, valueGetter, dontReplaceSlash, replacementCharacter) {
 		static async evalTemplateVariable(template, variableName, valueGetter, dontReplaceSlash, replacementCharacter) {
-			const replaceRegExp = new RegExp("{\\s*" + variableName.replace(/\W|_/g, "[$&]") + "\\s*}", "g");
-			if (template && template.match(replaceRegExp)) {
-				let value = await valueGetter();
-				if (!dontReplaceSlash) {
-					value = value.replace(/\/+/g, replacementCharacter);
+			let maxLength;
+			if (template) {
+				const regExpVariable = "{\\s*" + variableName.replace(/\W|_/g, "[$&]") + "\\s*}";
+				let replaceRegExp = new RegExp(regExpVariable + "\\[\\d+\\]", "g");
+				if (template.match(replaceRegExp)) {
+					const matchedLength = template.match(replaceRegExp)[0];
+					maxLength = Number(matchedLength.match(/\[(\d+)\]$/)[1]);
+					if (isNaN(maxLength) || maxLength <= 0) {
+						maxLength = null;
+					}
+				} else {
+					replaceRegExp = new RegExp(regExpVariable, "g");
+				}
+				if (template.match(replaceRegExp)) {
+					let value = await valueGetter();
+					if (!dontReplaceSlash) {
+						value = value.replace(/\/+/g, replacementCharacter);
+					}
+					if (maxLength) {
+						value = await util.truncateText(value, maxLength);
+					}
+					return template.replace(replaceRegExp, value);
 				}
 				}
-				return template.replace(replaceRegExp, value);
 			}
 			}
 			return template;
 			return template;
 		}
 		}