Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions morebits.js
Original file line number Diff line number Diff line change
Expand Up @@ -2621,6 +2621,7 @@ Morebits.wiki.page = function(pageName, status) {
// backing fields for public properties
pageName: pageName,
pageExists: false,
hatnoteRegex: null,
editSummary: null,
changeTags: null,
testActions: null, // array if any valid actions
Expand Down Expand Up @@ -2996,6 +2997,70 @@ Morebits.wiki.page = function(pageName, status) {
ctx.pageText = pageText;
};

/** Blank page based on content model */
this.blankPage = function() {
if (ctx.contentModel === 'json') {
ctx.pageText = '{}';
} else if (['Scribunto', 'javascript', 'css', 'sanitized-css', 'wikitext'].indexOf(ctx.contentModel) !== -1) {
ctx.pageText = '';
} else {
ctx.statusElement.error('Internal error: Unsupported content model: ' + ctx.contentModel);
}
};

/**
* Prepend maintenance tag based on content model
*
* @param {string} tag
*/
this.addMaintenanceTag = function(tag) {
if (!ctx.pageLoaded) {
ctx.statusElement.error('Internal error: cannot add maintenance tag that the page has not been loaded!');
return;
}

if (ctx.contentModel === 'Scribunto') {
// Scribunto isn't parsed like wikitext, so CSD templates on modules need special handling to work
var equals = '';
while (tag.indexOf(']' + equals + ']') !== -1) {
equals += '=';
}
ctx.pageText = "require('Module:Module wikitext')._addText([" + equals + '[' + tag + ']' + equals + ']);\n' + ctx.pageText;
} else if (['javascript', 'css', 'sanitized-css'].indexOf(ctx.contentModel) !== -1) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

要是內容模型是text好像也可以這樣加

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不確定你講的 1. text是否指wikitext 2. 是否指wikitext可同javascript處理,但總之我覺得不行。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

指的是 https://w.wiki/3R8U 的內容模組 "text"

@a2569875 a2569875 Jun 1, 2021

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

見此 https://zh.wikipedia.org/wiki/User:A2569875/FakeText.txt
頁面內容模型 : 純文字

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建議讓其格式為 /* _addText : "tag" */ 以顯示模板。

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我想等方針正式通過再根據方針處理,以免又有什麼變動。

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Likewise for JS/CSS pages
ctx.pageText = '/* _addText: ' + tag.replace(/\*\//g, '*/') + ' */\n' + ctx.pageText;
} else if (ctx.contentModel === 'json') {
try {
var pageJson = JSON.parse(ctx.pageText);
if (pageJson instanceof Array) {
pageJson.unshift({
_addText: tag
});
} else {
if (Object.prototype.hasOwnProperty.call(pageJson, '_addText')) {
pageJson._addText = tag + '\n' + pageJson._addText;
} else {
pageJson = $.extend({_addText: tag}, pageJson);
}
}
ctx.pageText = JSON.stringify(pageJson);
} catch (e) {
ctx.statusElement.error('Internal error: failed to parse page text into json!');
}
} else if (ctx.contentModel === 'wikitext') {
if (!ctx.hatnoteRegex) {
ctx.statusElement.error('Internal error: hatnoteRegex is not set!');
return;
}

// Insert tag after short description or any hatnotes
var wikipage = new Morebits.wikitext.page(ctx.pageText);
ctx.pageText = wikipage.insertAfterTemplates(tag + '\n', ctx.hatnoteRegex).getText();
} else {
ctx.statusElement.error('Internal error: Unsupported content model: ' + ctx.contentModel);
}
};

/** @param {string} appendText - Text that will be appended to the page when `append()` is called */
this.setAppendText = function(appendText) {
ctx.editMode = 'append';
Expand Down Expand Up @@ -3026,6 +3091,15 @@ Morebits.wiki.page = function(pageName, status) {


// Edit-related setter methods:
/**
* Set hatnoteRegex, used in addMaintenanceTag
*
* @param {string} regex
*/
this.setHatnoteRegex = function(regex) {
ctx.hatnoteRegex = regex;
};

/**
* Set the edit summary that will be used when `save()` is called.
* Unnecessary if editMode is 'new' and newSectionTitle is provided.
Expand Down