allow unusedtitle macro to use the prefix parameter and fix wiki.generateNewTitle() (#5361)

This commit is contained in:
Mario Pietsch
2021-05-02 19:26:50 +01:00
committed by GitHub
parent 3f98686153
commit cf56a17f28
6 changed files with 154 additions and 18 deletions
@@ -2,6 +2,7 @@
title: $:/core/modules/macros/unusedtitle.js
type: application/javascript
module-type: macro
Macro to return a new title that is unused in the wiki. It can be given a name as a base.
\*/
(function(){
@@ -10,25 +11,25 @@ Macro to return a new title that is unused in the wiki. It can be given a name a
/*global $tw: false */
"use strict";
/*
Information about this macro
*/
exports.name = "unusedtitle";
exports.params = [
{name: "baseName"},
{name: "options"}
{name: "separator"},
{name: "template"}
];
/*
Run the macro
*/
exports.run = function(baseName, options) {
exports.run = function(baseName,separator,template) {
separator = separator || " ";
if(!baseName) {
baseName = $tw.language.getString("DefaultNewTiddlerTitle");
}
return this.wiki.generateNewTitle(baseName, options);
// $tw.wiki.generateNewTitle = function(baseTitle,options)
// options.prefix must be a string!
return this.wiki.generateNewTitle(baseName, {"prefix": separator, "template": template});
};
})();
+41
View File
@@ -294,6 +294,47 @@ exports.slowInSlowOut = function(t) {
return (1 - ((Math.cos(t * Math.PI) + 1) / 2));
};
exports.formatTitleString = function(template,options) {
var base = options.base || "",
separator = options.separator || "",
counter = options.counter || "";
var result = "",
t = template,
matches = [
[/^\$basename\$/i, function() {
return base;
}],
[/^\$count:(\d+)\$/i, function(match) {
return $tw.utils.pad(counter,match[1]);
}],
[/^\$separator\$/i, function() {
return separator;
}],
[/^\$count\$/i, function() {
return counter + "";
}]
];
while(t.length){
var matchString = "";
$tw.utils.each(matches, function(m) {
var match = m[0].exec(t);
if(match) {
matchString = m[1].call(null,match);
t = t.substr(match[0].length);
return false;
}
});
if(matchString) {
result += matchString;
} else {
result += t.charAt(0);
t = t.substr(1);
}
}
result = result.replace(/\\(.)/g,"$1");
return result;
};
exports.formatDateString = function(date,template) {
var result = "",
t = template,
+15 -5
View File
@@ -190,15 +190,25 @@ exports.getChangeCount = function(title) {
/*
Generate an unused title from the specified base
options.prefix must be a string
*/
exports.generateNewTitle = function(baseTitle,options) {
options = options || {};
var c = 0,
title = baseTitle;
while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) {
title = baseTitle +
(options.prefix || " ") +
(++c);
title = baseTitle,
template = options.template,
prefix = (typeof(options.prefix) === "string") ? options.prefix : " ";
if (template) {
// "count" is important to avoid an endless loop in while(...)!!
template = (/\$count:?(\d+)?\$/i.test(template)) ? template : template + "$count$";
title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":c});
while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) {
title = $tw.utils.formatTitleString(template,{"base":baseTitle,"separator":prefix,"counter":(++c)});
}
} else {
while(this.tiddlerExists(title) || this.isShadowTiddler(title) || this.findDraft(title)) {
title = baseTitle + prefix + (++c);
}
}
return title;
};