Bläddra i källkod

added new variables

Gildas 7 år sedan
förälder
incheckning
7a9155159e
2 ändrade filer med 28 tillägg och 12 borttagningar
  1. 8 3
      extension/ui/pages/help.html
  2. 20 9
      lib/single-file/single-file-core.js

+ 8 - 3
extension/ui/pages/help.html

@@ -122,7 +122,12 @@
 						<p>The template allows you to customize the file name of saved pages. You can use any valid character and "/" to
 							create sub-folders. You can also use the variables in the list below anywhere in the template.</p>
 						<ul>
-							<li><code>{page-title}</code>: the title of the page or the last segment if not found</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-description}</code>: the description of the page</li>
+							<li><code>{page-author}</code>: the author of the page</li>
+							<li><code>{page-creator}</code>: the creator of the page</li>
+							<li><code>{page-publisher}</code>: the publisher of the page</li>
 							<li><code>{datetime-iso}</code>: the save date and time in the ISO format (e.g. "2018-09-15T22_38_26_317Z")</li>
 							<li><code>{datetime-utc}</code>: the save date and time in UTC format (e.g. "Sat, 15 Sep 2018 22_38_26 GMT")</li>
 							<li><code>{datetime-locale}</code>: the localized value of the date and time (e.g. "9_16_2018, 12_54_31 AM")</li>
@@ -138,7 +143,7 @@
 							<li><code>{year-locale}</code>: the localized value of the year (e.g. "2018")</li>
 							<li><code>{url-href}</code>: the URL of the page (e.g. "http_example.com")</li>
 							<li><code>{url-pathname}</code>: the path name of the URL (e.g. "category_index.html")</li>
-							<li><code>{url-last-segment}</code>: the last part of the pathname without the extension or the host if not
+							<li><code>{url-last-segment}</code>: the last part of the pathname (without the extension) or the host if not
 								found (e.g. "index")</li>
 							<li><code>{url-protocol}</code>: the protocol of the URL (e.g. "https")</li>
 							<li><code>{url-host}</code>: the host name + the port of the URL (e.g. "example.com_8080")</li>
@@ -148,7 +153,7 @@
 							<li><code>{url-password}</code>: the password of the URL (e.g. "qwerty123")</li>
 							<li><code>{url-search}</code>: the search string of the URL (e.g. "filter-date=today")</li>
 							<li><code>{url-hash}</code>: the hash of the URL (e.g. "chapter-2")</li>
-							<li><code>{tab-id}</code>: the id of the tab (e.g. "326")</li>
+							<li><code>{tab-id}</code>: the unique identifier of the tab (e.g. "326")</li>
 							<li><code>{digest-sha-256}</code>: the SHA-256 hash value of the entire page content (e.g.
 								e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855)</li>
 							<li><code>{digest-sha-384}</code>: the SHA-384 hash value of the entire page content</li>

+ 20 - 9
lib/single-file/single-file-core.js

@@ -300,12 +300,17 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				}
 			}
 			const titleElement = this.doc.querySelector("title");
-			if (titleElement) {
-				this.options.title = titleElement.textContent.trim();
-			} else {
-				this.options.title = "";
-			}
-			const matchTitle = this.baseURI.match(/([^/]*)\/?(\.html?.*)$/) || this.baseURI.match(/\/\/([^/]*)\/?$/);
+			this.options.title = titleElement ? titleElement.textContent.trim() : "";
+			this.options.info = {};
+			const descriptionElement = this.doc.querySelector("meta[name=description]");
+			this.options.info.description = descriptionElement ? descriptionElement.content.trim() : "";
+			this.options.info.lang = this.doc.documentElement.lang;
+			const authorElement = this.doc.querySelector("meta[name=author]");
+			this.options.info.author = authorElement ? authorElement.content.trim() : "";
+			const creatorElement = this.doc.querySelector("meta[name=creator]");
+			this.options.info.creator = creatorElement ? creatorElement.content.trim() : "";
+			const publisherElement = this.doc.querySelector("meta[name=publisher]");
+			this.options.info.publisher = creatorElement ? publisherElement.content.trim() : "";
 			const url = new URL(this.baseURI);
 			let size;
 			if (this.options.displayStats) {
@@ -318,6 +323,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				this.stats.add("discarded", "HTML bytes", size - contentSize);
 			}
 			const filename = await DomProcessorHelper.getFilename(this.options, content);
+			const matchTitle = this.baseURI.match(/([^/]*)\/?(\.html?.*)$/) || this.baseURI.match(/\/\/([^/]*)\/?$/);
 			return {
 				stats: this.stats.data,
 				title: this.options.title || (this.baseURI && matchTitle ? matchTitle[1] : (url.hostname ? url.hostname : "")),
@@ -787,7 +793,12 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			let filename = options.filenameTemplate;
 			const date = new Date();
 			const url = new URL(options.url);
-			filename = await DomUtil.evalTemplateVariable(filename, "page-title", () => options.title);
+			filename = await DomUtil.evalTemplateVariable(filename, "page-title", () => options.title || "No title");
+			filename = await DomUtil.evalTemplateVariable(filename, "page-language", () => options.info.lang || "No language");
+			filename = await DomUtil.evalTemplateVariable(filename, "page-description", () => options.info.description || "No description");
+			filename = await DomUtil.evalTemplateVariable(filename, "page-author", () => options.info.author || "No author");
+			filename = await DomUtil.evalTemplateVariable(filename, "page-creator", () => options.info.creator || "No creator");
+			filename = await DomUtil.evalTemplateVariable(filename, "page-publisher", () => options.info.publisher || "No publisher");
 			filename = await DomUtil.evalTemplateVariable(filename, "datetime-iso", () => date.toISOString());
 			filename = await DomUtil.evalTemplateVariable(filename, "date-iso", () => date.toISOString().split("T")[0]);
 			filename = await DomUtil.evalTemplateVariable(filename, "time-iso", () => date.toISOString().split("T")[1].split("Z")[0]);
@@ -811,7 +822,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			filename = await DomUtil.evalTemplateVariable(filename, "url-protocol", () => url.protocol);
 			filename = await DomUtil.evalTemplateVariable(filename, "url-search", () => url.search.substring(1));
 			filename = await DomUtil.evalTemplateVariable(filename, "url-username", () => url.username);
-			filename = await DomUtil.evalTemplateVariable(filename, "tab-id", () => String(options.tabId || ""));
+			filename = await DomUtil.evalTemplateVariable(filename, "tab-id", () => String(options.tabId || "No tab id"));
 			filename = await DomUtil.evalTemplateVariable(filename, "url-last-segment", () => DomUtil.getLastSegment(url));
 			filename = await DomUtil.evalTemplateVariable(filename, "digest-sha-256", async () => DOM.digest("SHA-256", content));
 			filename = await DomUtil.evalTemplateVariable(filename, "digest-sha-384", async () => DOM.digest("SHA-384", content));
@@ -824,7 +835,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				filename = filename.substring(0, 192 - extension.length) + "…" + extension;
 			}
 			if (!filename) {
-				filename = "Untitled page";
+				filename = "Unnamed page";
 			}
 			return filename;
 		}