User:DivermanAU/common.js: Difference between revisions
Jump to navigation
Jump to search
Content deleted Content added
m Ajraddatz moved page User:Diverman/common.js to User:DivermanAU/common.js without leaving a redirect: Automatically moved page while renaming the user "Diverman" to "DivermanAU" |
Pathoschild (talk | contribs) updated obsolete regex menu framework to newer TemplateScript |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
/** |
|||
//<pre> |
|||
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor. |
|||
//To get these to work, required gadget regex turned ON |
|||
* @see https://meta.wikimedia.org/wiki/TemplateScript |
|||
function rmflinks() { |
|||
* @update-token [[File:pathoschild/templatescript.js]] |
|||
regexTool('{{rh}}', 'RunningHeader()'); |
|||
*/ |
|||
regexTool('clean up','cleanup()'); |
|||
// <nowiki> |
|||
regexTool('nop','nop()'); |
|||
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() { |
|||
regexTool('DefSort Subpage','defsortsubpage()'); |
|||
/********* |
|||
** Register scripts |
|||
*********/ |
|||
//Main namespace loads |
|||
pathoschild.TemplateScript.add([ |
|||
if (isMainspace) { |
|||
// all namespace |
|||
regexTool('pn [[//]]','PrevNext1()'); |
|||
//{ name: '{{rh}}', script: RunningHeader }, |
|||
regexTool('Pages','tag_pages()'); |
|||
{ name: 'clean up', script: cleanup }, |
|||
regexTool('Pages++','tag_pages_section()'); |
|||
{ name: 'nop', script: nop }, |
|||
} |
|||
{ name: 'DefSort Subpage', script: defsortsubpage }, |
|||
} |
|||
// main namespace |
|||
function PrevNext1() { |
|||
{ name: 'pn [[//]]', script: PrevNext1, forNamespaces: 0 }, |
|||
var prevnextlink = '[[..//]]'; |
|||
{ name: 'Pages', script: tag_pages, forNamespaces: 0 }, |
|||
editbox.value = editbox.value.replace(/\| previous \= \n \| next \= /, '| previous \= ' + prevnextlink +'\n \| next \= ' + prevnextlink ); |
|||
{ name: 'Pages++', script: tag_pages_section, forNamespaces: 0 } |
|||
} |
|||
]); |
|||
//subst: #tag and some fields |
|||
/********* |
|||
function tag_pages() { |
|||
** Define scripts |
|||
var selStart = editbox.selectionStart; |
|||
*********/ |
|||
var selEnd = editbox.selectionEnd; |
|||
function PrevNext1(editor) { |
|||
var pre = editbox.value.substring(selStart, selEnd); |
|||
var prevnextlink = '[[..//]]'; |
|||
var pages= '<pages index="Diverman.djvu" from="" to=""></pages>'; |
|||
editor.replace(/\| previous \= \n \| next \= /, '| previous \= ' + prevnextlink + '\n \| next \= ' + prevnextlink); |
|||
editbox.value = editbox.value.substring(0, selStart) |
|||
} |
|||
+ pages |
|||
+ editbox.value.substring(selEnd); |
|||
//subst: #tag and some fields |
|||
editbox.focus(); |
|||
function tag_pages(editor) { |
|||
editbox.setSelectionRange(selStart+post.length, selStart+post.length); |
|||
editor.replaceSelection(function(pre) { |
|||
} |
|||
return '<pages index="Diverman.djvu" from="" to=""></pages>'; |
|||
}); |
|||
} |
|||
function tag_pages_section() { |
|||
var selStart = editbox.selectionStart; |
|||
function tag_pages_section(editor) { |
|||
var selEnd = editbox.selectionEnd; |
|||
editor.replaceSelection(function(pre) { |
|||
var pre = editbox.value.substring(selStart, selEnd); |
|||
// |
//return '{{subst'+':#tag:pages||index="{{subst'+':BASEPAGENAME}}.djvu" from= to= fromsection="{{subst'+':SUBPAGENAME}}" tosection="{{subst'+':SUBPAGENAME}}"}}'; |
||
return '<pages index="Diverman.djvu" from="" to="" fromsection="common.js" tosection="common.js"></pages>'; |
|||
}); |
|||
editbox.value = editbox.value.substring(0, selStart) |
|||
} |
|||
+ pages |
|||
+ editbox.value.substring(selEnd); |
|||
function defsortsubpage(editor) { |
|||
editbox.focus(); |
|||
editor.append('{{DEFAULTSORT:{{subst:SUBPAGENAME}}}}'); |
|||
editbox.setSelectionRange(selStart+post.length, selStart+post.length); |
|||
} |
} |
||
function |
function cleanup(editor) { |
||
var headerbox = editor.forField('#wpHeaderTextbox'); |
|||
editbox.value = editbox.value + '{{DEFAULTSORT:common.js}}' |
|||
var footerbox = editor.forField('#wpFooterTextbox'); |
|||
} |
|||
editor |
|||
function cleanup() { |
|||
// Digitized by Google (kill) |
|||
var editbox = document.getElementsByName('wpTextbox1')[0]; |
|||
.replace(/Digitized[\s\n]+by[^\n]+\n(Google)?/, '') |
|||
var headerbox = document.getElementsByName('wpHeaderTextbox')[0]; |
|||
var footerbox = document.getElementsByName('wpFooterTextbox')[0]; |
|||
// remove trailing spaces at the end of each line |
|||
.replace(/ +\n/g, '\n') |
|||
// Digitized by Google (kill) |
|||
editbox.value = editbox.value.replace(/Digitized[\s\n]+by[^\n]+\n(Google)?/, ''); |
|||
// remove trailing whitespace preceding a hard line break |
|||
.replace(/ +<br *\/?>/g, '<br />') |
|||
// remove trailing spaces at the end of each line |
|||
editbox.value = editbox.value.replace(/ +\n/g, '\n'); |
|||
// remove trailing whitespace at the end of page text |
|||
.replace(/\s+$/g, '') |
|||
// remove trailing whitespace preceding a hard line break |
|||
editbox.value = editbox.value.replace(/ +<br *\/?>/g, '<br />'); |
|||
// remove trailing spaces at the end of refs |
|||
.replace(/ +<\/ref>/g, '</ref>') |
|||
// remove trailing whitespace at the end of page text |
|||
editbox.value = editbox.value.replace(/\s+$/g, ''); |
|||
// remove trailing spaces at the end of template calls |
|||
.replace(/ +}}/g, '}}') |
|||
// remove trailing spaces at the end of refs |
|||
editbox.value = editbox.value.replace(/ +<\/ref>/g, '</ref>'); |
|||
// convert double-hyphen to mdash (avoiding breaking HTML comment syntax) |
|||
.replace(/([^\!])--([^>])/g, '$1—$2') |
|||
// remove trailing spaces at the end of template calls |
|||
editbox.value = editbox.value.replace(/ +}}/g, '}}'); |
|||
// remove spacing around mdash, but only if it has spaces on both sides |
|||
// (we don't want to remove the trailing space from "...as follows:— ", |
|||
// convert double-hyphen to mdash (avoiding breaking HTML comment syntax) |
|||
// bearing in mind that the space will already be gone if at end of line). |
|||
editbox.value = editbox.value.replace(/([^\!])--([^>])/g, '$1—$2'); |
|||
.replace(/ +— +/g, '—') |
|||
// remove spacing around mdash, but only if it has spaces on both sides |
|||
// join words that are hyphenated across a line break |
|||
// (we don't want to remove the trailing space from "...as follows:— ", |
|||
// (but leave "|-" table syntax alone) |
|||
// bearing in mind that the space will already be gone if at end of line). |
|||
.replace(/([^\|])-\n/g, '$1'); |
|||
// stuff to do only if the page doesn't contain a <poem> tag: |
|||
// join words that are hyphenated across a line break |
|||
if (!editor.contains('<poem>')) { |
|||
// (but leave "|-" table syntax alone) |
|||
editor |
|||
editbox.value = editbox.value.replace(/([^\|])-\n/g, '$1'); |
|||
// remove single line breaks; preserve multiple. |
|||
// but not if there's a tag, template or table syntax either side of the line break |
|||
// stuff to do only if the page doesn't contain a <poem> tag: |
|||
.replace(/([^>}\n])\n([^<{\|\n])/g, '$1 $2') |
|||
if (-1 == editbox.value.indexOf("<poem>")) { |
|||
// remove single line breaks; preserve multiple. |
|||
// collapse sequences of spaces into a single space |
|||
// but not if there's a tag, template or table syntax either side of the line break |
|||
.replace(/ +/g, ' '); |
|||
} |
|||
// collapse sequences of spaces into a single space |
|||
editor |
|||
editbox.value = editbox.value.replace(/ +/g, ' '); |
|||
// remove unwanted spaces around punctuation marks |
|||
} |
|||
.replace(/ ([;:\?!,])/g, '$1') |
|||
// remove unwanted spaces around punctuation marks |
|||
//OCR fixes |
|||
editbox.value = editbox.value.replace(/ ([;:\?!,])/g, '$1'); |
|||
// convert i9 to 19, etc. |
|||
.replace(/[il]([0-9])/g, '1$1') |
|||
//OCR fixes |
|||
// "the", "them", "their", etcetera |
|||
// convert i9 to 19, etc. |
|||
.replace(/tlie/g, 'the') |
|||
// "U" -> "ll" when preceded by a lowercase letter. |
|||
// "the", "them", "their", etcetera |
|||
.replace(/([a-z])U/g, '$1ll') |
|||
// {{c}} to {{center}} |
|||
// "U" -> "ll" when preceded by a lowercase letter. |
|||
.replace(/{{c\|/g, '{{center|'); |
|||
headerbox.replace(/{{c\|/g, '{{center|'); |
|||
footerbox.replace(/{{c\|/g, '{{center|'); |
|||
headerbox.value = headerbox.value.replace(/{{c\|/g, '{{center|'); |
|||
// {{rh}} to {{RunningHeader}} |
|||
footerbox.value = footerbox.value.replace(/{{c\|/g, '{{center|'); |
|||
headerbox.replace(/{{rh\|/gi, '{{RunningHeader|'); |
|||
// {{rh}} to {{RunningHeader}} |
|||
editor |
|||
headerbox.value = headerbox.value.replace(/{{rh\|/gi, '{{RunningHeader|'); |
|||
//{{hws}} & {{hwe}} expanded |
|||
.replace(/{{hws\|/g, '{{hyphenated word start|') |
|||
//{{hws}} & {{hwe}} expanded |
|||
.replace(/{{hwe\|/g, '{{hyphenated word end|') |
|||
editbox.value = editbox.value.replace(/{{hwe\|/g, '{{hyphenated word end|'); |
|||
// expand diacritical templates |
|||
.replace(/{{((ae|oe|\w[:`'~^-]))}}/g, '{{subst' + ':$1}}'); |
|||
// expand diacritical templates |
|||
} |
|||
editbox.value = editbox.value.replace(/{{((ae|oe|\w[:`'~^-]))}}/g, '{{subst'+':$1}}'); |
|||
//end of cleanup |
|||
} |
|||
//end of cleanup |
|||
// nop at bottom of editbox |
|||
function nop(editor) { |
|||
// nop at bottom of editbox |
|||
editor.append('\n{{nop}}'); |
|||
function nop() { |
|||
} |
|||
editbox.value += '\n{{nop}}'; |
|||
} |
}); |
||
// </nowiki> |
|||
function defsortsubpage() { |
|||
editbox.value = editbox.value + '{{DEFAULTSORT:{{subst:SUBPAGENAME}}}}' |
|||
} |
|||
//</pre> |
Revision as of 16:35, 12 September 2015
/**
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
* @see https://meta.wikimedia.org/wiki/TemplateScript
* @update-token [[File:pathoschild/templatescript.js]]
*/
// <nowiki>
$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
/*********
** Register scripts
*********/
pathoschild.TemplateScript.add([
// all namespace
//{ name: '{{rh}}', script: RunningHeader },
{ name: 'clean up', script: cleanup },
{ name: 'nop', script: nop },
{ name: 'DefSort Subpage', script: defsortsubpage },
// main namespace
{ name: 'pn [[//]]', script: PrevNext1, forNamespaces: 0 },
{ name: 'Pages', script: tag_pages, forNamespaces: 0 },
{ name: 'Pages++', script: tag_pages_section, forNamespaces: 0 }
]);
/*********
** Define scripts
*********/
function PrevNext1(editor) {
var prevnextlink = '[[..//]]';
editor.replace(/\| previous \= \n \| next \= /, '| previous \= ' + prevnextlink + '\n \| next \= ' + prevnextlink);
}
//subst: #tag and some fields
function tag_pages(editor) {
editor.replaceSelection(function(pre) {
return '<pages index="Diverman.djvu" from="" to=""></pages>';
});
}
function tag_pages_section(editor) {
editor.replaceSelection(function(pre) {
//return '{{subst'+':#tag:pages||index="{{subst'+':BASEPAGENAME}}.djvu" from= to= fromsection="{{subst'+':SUBPAGENAME}}" tosection="{{subst'+':SUBPAGENAME}}"}}';
return '<pages index="Diverman.djvu" from="" to="" fromsection="common.js" tosection="common.js"></pages>';
});
}
function defsortsubpage(editor) {
editor.append('{{DEFAULTSORT:{{subst:SUBPAGENAME}}}}');
}
function cleanup(editor) {
var headerbox = editor.forField('#wpHeaderTextbox');
var footerbox = editor.forField('#wpFooterTextbox');
editor
// Digitized by Google (kill)
.replace(/Digitized[\s\n]+by[^\n]+\n(Google)?/, '')
// remove trailing spaces at the end of each line
.replace(/ +\n/g, '\n')
// remove trailing whitespace preceding a hard line break
.replace(/ +<br *\/?>/g, '<br />')
// remove trailing whitespace at the end of page text
.replace(/\s+$/g, '')
// remove trailing spaces at the end of refs
.replace(/ +<\/ref>/g, '</ref>')
// remove trailing spaces at the end of template calls
.replace(/ +}}/g, '}}')
// convert double-hyphen to mdash (avoiding breaking HTML comment syntax)
.replace(/([^\!])--([^>])/g, '$1—$2')
// remove spacing around mdash, but only if it has spaces on both sides
// (we don't want to remove the trailing space from "...as follows:— ",
// bearing in mind that the space will already be gone if at end of line).
.replace(/ +— +/g, '—')
// join words that are hyphenated across a line break
// (but leave "|-" table syntax alone)
.replace(/([^\|])-\n/g, '$1');
// stuff to do only if the page doesn't contain a <poem> tag:
if (!editor.contains('<poem>')) {
editor
// remove single line breaks; preserve multiple.
// but not if there's a tag, template or table syntax either side of the line break
.replace(/([^>}\n])\n([^<{\|\n])/g, '$1 $2')
// collapse sequences of spaces into a single space
.replace(/ +/g, ' ');
}
editor
// remove unwanted spaces around punctuation marks
.replace(/ ([;:\?!,])/g, '$1')
//OCR fixes
// convert i9 to 19, etc.
.replace(/[il]([0-9])/g, '1$1')
// "the", "them", "their", etcetera
.replace(/tlie/g, 'the')
// "U" -> "ll" when preceded by a lowercase letter.
.replace(/([a-z])U/g, '$1ll')
// {{c}} to {{center}}
.replace(/{{c\|/g, '{{center|');
headerbox.replace(/{{c\|/g, '{{center|');
footerbox.replace(/{{c\|/g, '{{center|');
// {{rh}} to {{RunningHeader}}
headerbox.replace(/{{rh\|/gi, '{{RunningHeader|');
editor
//{{hws}} & {{hwe}} expanded
.replace(/{{hws\|/g, '{{hyphenated word start|')
.replace(/{{hwe\|/g, '{{hyphenated word end|')
// expand diacritical templates
.replace(/{{((ae|oe|\w[:`'~^-]))}}/g, '{{subst' + ':$1}}');
}
//end of cleanup
// nop at bottom of editbox
function nop(editor) {
editor.append('\n{{nop}}');
}
});
// </nowiki>