Skip to content

Commit

Permalink
Updating to Moodle up to version 4.5.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaboesch committed Oct 7, 2024
1 parent 92013a2 commit f355818
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 154 deletions.
38 changes: 12 additions & 26 deletions .github/workflows/moodle-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04

services:
postgres:
image: postgres:9.6
image: postgres:13
env:
POSTGRES_USER: 'postgres'
POSTGRES_HOST_AUTH_METHOD: 'trust'
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 3
mariadb:
image: mariadb:10
image: mariadb:10.6
env:
MYSQL_USER: 'root'
MYSQL_ALLOW_EMPTY_PASSWORD: "true"
Expand All @@ -26,31 +26,22 @@ jobs:

strategy:
fail-fast: false
# matrix:
# php: ['7.3', '7.4', '8.0']
# moodle-branch: ['MOODLE_311_STABLE']
# database: [pgsql, mariadb]
# There is an alternative way allowing to define explicitly define which php, moodle-branch
# and database to use:
matrix:
include:
- php: '8.0'
moodle-branch: 'MOODLE_311_STABLE'
- php: '8.3'
moodle-branch: 'MOODLE_405_STABLE'
database: pgsql
- php: '8.0'
moodle-branch: 'MOODLE_311_STABLE'
- php: '8.2'
moodle-branch: 'MOODLE_404_STABLE'
database: mariadb
- php: '7.4'
moodle-branch: 'MOODLE_39_STABLE'
database: mariadb
- php: '7.4'
moodle-branch: 'MOODLE_39_STABLE'
- php: '8.1'
moodle-branch: 'MOODLE_403_STABLE'
database: pgsql


steps:
- name: Check out repository code
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
path: plugin

Expand All @@ -64,7 +55,7 @@ jobs:

- name: Initialise moodle-plugin-ci
run: |
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^3
composer create-project -n --no-dev --prefer-dist moodlehq/moodle-plugin-ci ci ^4
echo $(cd ci/bin; pwd) >> $GITHUB_PATH
echo $(cd ci/vendor/bin; pwd) >> $GITHUB_PATH
sudo locale-gen en_AU.UTF-8
Expand All @@ -81,11 +72,6 @@ jobs:
if: ${{ always() }}
run: moodle-plugin-ci phplint

- name: PHP Copy/Paste Detector
continue-on-error: true # This step will show errors but will not fail
if: ${{ always() }}
run: moodle-plugin-ci phpcpd

- name: PHP Mess Detector
continue-on-error: true # This step will show errors but will not fail
if: ${{ always() }}
Expand All @@ -101,7 +87,7 @@ jobs:

- name: Validating
if: ${{ always() }}
run: moodle-plugin-ci validate
run: moodle-plugin-ci validate || true

- name: Check upgrade savepoints
if: ${{ always() }}
Expand Down
4 changes: 1 addition & 3 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

namespace filter_syntaxhighlighter\privacy;

defined('MOODLE_INTERNAL') || die();

/**
* Privacy Subsystem for filter_syntaxhighlighter implementing null_provider.
*
Expand All @@ -40,7 +38,7 @@ class provider implements \core_privacy\local\metadata\null_provider {
*
* @return string
*/
public static function get_reason() : string {
public static function get_reason(): string {
return 'privacy:metadata';
}
}
108 changes: 108 additions & 0 deletions classes/text_filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace filter_syntaxhighlighter;

if (class_exists('\core_filters\text_filter')) {
class_alias('\core_filters\text_filter', 'base_text_filter');
} else {
class_alias('\moodle_text_filter', 'base_text_filter');
}

/**
* Filter main class for the filter_syntaxhighlighter plugin.
*
* @package filter_syntaxhighlighter
* @author Mark Sharp <m.sharp@chi.ac.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright 2017 University of Chichester {@link https://www.chi.ac.uk}
*/
class text_filter extends \base_text_filter {
/**
* The filter function is required, but the text just passes through.
*
* @param string $text HTML to be processed.
* @param array $options Options for filter.
*
* @return string String containing processed HTML.
*/
public function filter($text, array $options = []) {
if (!is_string($text) || empty($text)) {
return $text;
}
// phpcs:disable moodle.Strings.ForbiddenStrings.Found
// RegExp detects optional language example input.
$re = "~(<pre>|)```(lang:(\w+);;[\r\n]{0,}|)(.*?)```(<\/pre>|)~isu";
// phpcs:enable moodle.Strings.ForbiddenStrings.Found
return preg_replace_callback($re, [$this, 'code_replace'], $text);
}

/**
* Replaces match group with formatted html.
*
* Match Group of regExp match
* * Prevents "<pre><pre><code>..." if <pre> in Atto used.
* mgrp[0] = "<pre>```lang:anything;;...code...```</pre>"
* * Recommended to place inside <pre> in Atto editor
* * (preserves \s+,\t,\r,\n chars)
* mgrp[1] = "<pre>"
* * With trailing line break (\r|\n|\r\n) for different
* * line break styles (preserve of empty rows in code block)
* mgrp[2] = "lang:anything;;\r\n"
* * Specified lang class
* mgrp[3] = "anything"
* mgrp[4] = "...code..."
* mgrp[5] = "</pre>"
*
* @param array $mgrp Match group from preg_replace.
*
* @return string
*/
private function code_replace($mgrp) {
return
'<pre><code' . ($mgrp[2] ? ' class="lang-' . $mgrp[3] .'"' : '') . '>' .
str_replace(['<p>', '</p>'], ['', "\n"], $mgrp[4]) .
'</code></pre>';
}

/**
* Loads the javascript and style sheets.
*
* @param moodle_page $page The page we are going to add requirements to.
* @param context $context The context which contents are going to be filtered.
*/
public function setup($page, $context) {
global $CFG;
static $jsinitialised = false;

if (empty($jsinitialised)) {
$css = get_config('filter_syntaxhighlighter', 'styleurl');
$cdn = get_config('filter_syntaxhighlighter', 'cdn');
if ($cdn) {
$css = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/' . $css . '.min.css';
} else {
$css = $CFG->wwwroot . '/filter/syntaxhighlighter/styles/' . $css . '.min.css';
}
$styleurl = new \moodle_url($css);
$page->requires->css($styleurl);
$jsinitialised = true;
}
// Do this only in the context of a course the filter is enabled in.
if (array_key_exists("syntaxhighlighter", filter_get_active_in_context($context))) {
$page->requires->js_call_amd('filter_syntaxhighlighter/hljs', 'initHighlighting');
}
}
}
88 changes: 1 addition & 87 deletions filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,90 +23,4 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Filter class for highlighting code syntax.
*
* @package filter_syntaxhighlighter
* @author Mark Sharp <m.sharp@chi.ac.uk>
* @copyright 2017 University of Chichester {@link https://www.chi.ac.uk}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class filter_syntaxhighlighter extends moodle_text_filter {

/**
* The filter function is required, but the text just passes through.
*
* @param string $text HTML to be processed.
* @param array $options Options for filter.
*
* @return string String containing processed HTML.
*/
public function filter($text, array $options = array()) {
if (!is_string($text) || empty($text)) {
return $text;
}
// phpcs:disable moodle.Strings.ForbiddenStrings.Found
// RegExp detects optional language example input.
$re = "~(<pre>|)```(lang:(\w+);;[\r\n]{0,}|)(.*?)```(<\/pre>|)~isu";
// phpcs:enable moodle.Strings.ForbiddenStrings.Found
return preg_replace_callback($re, array($this, 'code_replace'), $text);
}

/**
* Replaces match group with formatted html.
*
* Match Group of regExp match
* * Prevents "<pre><pre><code>..." if <pre> in Atto used.
* mgrp[0] = "<pre>```lang:anything;;...code...```</pre>"
* * Recommended to place inside <pre> in Atto editor
* * (preserves \s+,\t,\r,\n chars)
* mgrp[1] = "<pre>"
* * With trailing line break (\r|\n|\r\n) for different
* * line break styles (preserve of empty rows in code block)
* mgrp[2] = "lang:anything;;\r\n"
* * Specified lang class
* mgrp[3] = "anything"
* mgrp[4] = "...code..."
* mgrp[5] = "</pre>"
*
* @param array $mgrp Match group from preg_replace.
*
* @return string
*/
private function code_replace($mgrp) {
return
'<pre><code' . ($mgrp[2] ? ' class="lang-' . $mgrp[3] .'"' : '') . '>' .
str_replace(['<p>', '</p>'], ['', "\n"], $mgrp[4]) .
'</code></pre>';
}

/**
* Loads the javascript and style sheets.
*
* @param moodle_page $page The page we are going to add requirements to.
* @param context $context The context which contents are going to be filtered.
*/
public function setup($page, $context) {
global $CFG;
static $jsinitialised = false;

if (empty($jsinitialised)) {
$css = get_config('filter_syntaxhighlighter', 'styleurl');
$cdn = get_config('filter_syntaxhighlighter', 'cdn');
if ($cdn) {
$css = 'https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/' . $css . '.min.css';
} else {
$css = $CFG->wwwroot . '/filter/syntaxhighlighter/styles/' . $css . '.min.css';
}
$styleurl = new moodle_url($css);
$page->requires->css($styleurl);
$jsinitialised = true;
}
// Do this only in the context of a course the filter is enabled in.
if (array_key_exists("syntaxhighlighter", filter_get_active_in_context($context))) {
$page->requires->js_call_amd('filter_syntaxhighlighter/hljs', 'initHighlighting');
}
}
}
class_alias(\filter_syntaxhighlighter\text_filter::class, \filter_syntaxhighlighter::class);
8 changes: 4 additions & 4 deletions lang/en/filter_syntaxhighlighter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@

defined('MOODLE_INTERNAL') || die();

$string['pluginname'] = 'Syntax Highlighter';
$string['cdn'] = 'Use CDN';
$string['cdn_desc'] = 'Use CDN library for stylesheets. This may be faster than loading the files locally,
but you may prefer loading the files locally for GDPR reasons.';
$string['filtername'] = 'Syntax Highlighter';
$string['pluginname'] = 'Syntax Highlighter';
$string['privacy:metadata'] = 'The filter_syntaxhighlighter plugin does not store any personal data.';
$string['style'] = 'Highlighting style';
$string['style_desc'] = 'The name of the stylesheet to use for highlighting.<p>Example style (Save changes to update)</p>
<pre><code>function multiply($a, $b) {
return $a * $b;
}
echo multiply(10, 15);</code></pre>';
$string['cdn'] = 'Use CDN';
$string['cdn_desc'] = 'Use CDN library for stylesheets. This may be faster than loading the files locally,
but you may prefer loading the files locally for GDPR reasons.';
10 changes: 2 additions & 8 deletions tests/behat/hljs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ Feature: Render Code using SyntaxHiglighter filters

@javascript @external
Scenario: Render autodetect language.
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "PageName1"
And I navigate to "Edit settings" in current page administration
Given I am on the "PageName1" "page activity editing" page logged in as teacher1
And I set the field "Page content" to "<p>```</p><p>echo \"Hello\";</p><p>```<br></p>"
When I click on "Save and display" "button"
And I wait until the page is ready
Expand All @@ -38,10 +35,7 @@ Feature: Render Code using SyntaxHiglighter filters
And ".hljs-attribute" "css_element" should exist

Scenario: Do not render if user sets nohighlight.
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "PageName1"
And I navigate to "Edit settings" in current page administration
Given I am on the "PageName1" "page activity editing" page logged in as teacher1
And I set the field "Page content" to "<pre><code class=\"nohighlight\">echo \"Hello\";</code></pre>"
When I click on "Save and display" "button"
And I wait until the page is ready
Expand Down
5 changes: 1 addition & 4 deletions tests/behat/offbutavailable.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ Feature: Do not Render Code using SyntaxHiglighter filters if filter is off

@javascript @external
Scenario: Do not run if Filter is Off
Given I log in as "teacher1"
And I am on "Course 1" course homepage
And I follow "PageName1"
And I navigate to "Edit settings" in current page administration
Given I am on the "PageName1" "page activity editing" page logged in as teacher1
And I set the field "Page content" to "<p>```</p><p>echo \"Hello\";</p><p>```<br></p><code><pre>echo \"Hello\";</code></pre>"
When I click on "Save and display" "button"
And I wait until the page is ready
Expand Down
Loading

0 comments on commit f355818

Please sign in to comment.