Skip to content

Commit

Permalink
Update to scratchblocks v3.6.1, refactor code (#13)
Browse files Browse the repository at this point in the history
* Host scratchblocks locally

* Use v2 schema

* Refactor code
  • Loading branch information
apple502j authored Oct 5, 2022
1 parent 2cd8da5 commit 7dd5002
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
A simple MediaWiki extension for rendering Scratch Blocks used on Scratch 3.0.
A simple MediaWiki extension for rendering Scratch Blocks used on Scratch 3.0. Supports MediaWiki 1.29+.

Transforms `<scratchblocks>` tags inside wiki articles into `<pre class="blocks">` in the HTML, which are then rendered to scratch blocks using CSS and JS included in the page. Inline blocks are rendered with `<sb>` tags, and become `<code class="blocks">` tags.

Expand Down
15 changes: 4 additions & 11 deletions ScratchblockHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,10 @@ public static function sb4Setup(Parser $parser) {

public static function sb4RenderTagGeneric($input, array $args, $parser, $tag) {
self::sb4Setup($parser);
return (
'<'
. $tag
. ' class="blocks'
. (isset($args['version']) ? '-' . htmlspecialchars($args['version']) : '')
. '">'
. htmlspecialchars($input)
. '</'
. $tag
. '>'
);
return Html::element($tag, [
'class' => 'blocks' . (isset($args['version']) ? '-' . $args['version'] : ''),
$input
]);
}

// Output HTML for <scratchblocks> tag
Expand Down
31 changes: 27 additions & 4 deletions extension.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@
"run_scratchblocks4.js"
],
"styles": "/inline.css",
"dependencies": []
"dependencies": [
"ext.scratchBlocks4.sb",
"ext.scratchBlocks4.translations"
]
},
"ext.scratchBlocks4.sb": {
"scripts": [
"lib/scratchblocks.min.js"
]
},
"ext.scratchBlocks4.translations": {
"scripts": [
"lib/translations-all.js"
],
"dependencies": [
"ext.scratchBlocks4.sb"
]
}
},
"ResourceFileModulePaths": {
Expand All @@ -40,8 +56,15 @@
]
},
"config": {
"ScratchBlocks4Langs": [],
"ScratchBlocks4BlockVersion": "3.0"
"ScratchBlocks4Langs": {
"value": []
},
"ScratchBlocks4BlockVersion": {
"value": "3.0"
}
},
"manifest_version": 1
"manifest_version": 2,
"requires": {
"MediaWiki": ">= 1.29.0"
}
}
11 changes: 11 additions & 0 deletions lib/scratchblocks.min.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions lib/translations-all.js

Large diffs are not rendered by default.

28 changes: 9 additions & 19 deletions run_scratchblocks4.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
function run_scratchblocks() {
mw.hook('wikipage.content').add(function run_scratchblocks() {
var version = mw.config.get('wgScratchBlocks4BlockVersion');
var scale = 1;
// Note: the weak equality is intentional to allow '2' and 2 both specify sb2
if (version == 2 || version[0] == '2') { // to handle '2.0'
version = 'scratch2';
} else {
version = 'scratch3';
scale = 0.675;
}
var langs = ['en'].concat(mw.config.get('wgScratchBlocks4Langs'));
scratchblocks.renderMatching('pre.blocks', {languages: langs, style: version});
scratchblocks.renderMatching('code.blocks', {languages: langs, style: version, inline: true});
scratchblocks.renderMatching('pre[class^=blocks-3]', {languages: langs, style: 'scratch3'});
scratchblocks.renderMatching('code[class^=blocks-3]', {languages: langs, style: 'scratch3', inline: true});
scratchblocks.renderMatching('pre[class^=blocks-2]', {languages: langs, style: 'scratch2'});
scratchblocks.renderMatching('code[class^=blocks-2]', {languages: langs, style: 'scratch2', inline: true});
scratchblocks.renderMatching('pre.blocks', { languages: langs, style: version, scale: scale });
scratchblocks.renderMatching('code.blocks', { languages: langs, style: version, inline: true, scale: scale });
scratchblocks.renderMatching('pre[class^=blocks-3]', { languages: langs, style: 'scratch3', scale: 0.675 });
scratchblocks.renderMatching('code[class^=blocks-3]', { languages: langs, style: 'scratch3', inline: true, scale: 0.675 });
scratchblocks.renderMatching('pre[class^=blocks-2]', { languages: langs, style: 'scratch2' });
scratchblocks.renderMatching('code[class^=blocks-2]', { languages: langs, style: 'scratch2', inline: true });
var query = '[class^=blocks-3] .scratchblocks svg';
if (version === 'scratch3') {
query = '.blocks .scratchblocks svg, ' + query;
}
var items = document.querySelectorAll(query);
var item;
for (var i = items.length; i--;) {
item = items[i];
item.width.baseVal.value *= 0.675;
item.height.baseVal.value = item.viewBox.baseVal.height * 0.675;
}
}
$.getScript('https://scratchblocks.github.io/js/scratchblocks-v3.6.0-min.js').done(function(){
$.getScript('https://scratchblocks.github.io/js/translations-all-v3.6.0.js').done(function(){
mw.hook('wikipage.content').add(run_scratchblocks);
});
});

0 comments on commit 7dd5002

Please sign in to comment.