Skip to content

Commit e6e567d

Browse files
committed
working draft for reading snippets and their from scss files and their comments
1 parent 2936e04 commit e6e567d

File tree

6 files changed

+170
-119
lines changed

6 files changed

+170
-119
lines changed

classes/snippets.php

+150-17
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
namespace theme_boost_union;
1818

19+
use moodle_recordset;
20+
1921
/**
2022
* Class snippets
2123
*
@@ -24,17 +26,92 @@
2426
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2527
*/
2628
class snippets {
29+
/**
30+
* Constant for how many a Kilobyte are in bytes.
31+
*
32+
* @var int
33+
*/
34+
const KB_IN_BYTES = 1024;
35+
36+
/**
37+
* List of all available Snippet Meta File headers.
38+
*
39+
* @var array
40+
*/
41+
const SNIPPET_HEADERS = [
42+
'Snippet Title',
43+
'Goal',
44+
'Domain',
45+
'Description',
46+
'Scope',
47+
];
48+
49+
/**
50+
* Gets the snippet file based on the meta information.
51+
* @param mixed $key
52+
* @param mixed $source
53+
* @param mixed $domain
54+
* @return string|null
55+
*/
56+
public static function get_snippet_file($key, $source, $domain = '') {
57+
global $CFG;
58+
if ('theme_boost_union' === $source) {
59+
$file = $CFG->dirroot . sprintf('/theme/boost_union/snippets/builtin/%s.scss', $key);
60+
} else {
61+
return null;
62+
}
63+
return is_readable($file) ? $file : null;
64+
}
65+
66+
/**
67+
* TODO
68+
* @param mixed $key
69+
* @param mixed $source
70+
* @param mixed $domain
71+
* @return bool|string
72+
*/
73+
public static function get_snippet_css($key, $source, $domain = '') {
74+
// Get the snippets file, based on the source and domain.s
75+
$file = self::get_snippet_file($key, $source);
76+
77+
if (is_null($file)) {
78+
return;
79+
}
80+
$scss = file_get_contents( $file, false, null, 0);
81+
82+
return $scss;
83+
}
84+
2785
/**
2886
* Get a snippet defined in the code based on key and domain.
2987
* @param string $key
3088
* @param string $domain
3189
* @return mixed
3290
*/
33-
public static function get_snippet($key, $domain = 'theme_boost_union') {
91+
public static function get_snippet_meta($key, $source) {
3492
global $CFG;
35-
if ('theme_boost_union' == $domain) {
36-
return require_once($CFG->dirroot . sprintf('/theme/boost_union/snippets/builtin/%s.php', $key));
93+
94+
// Get the snippets file, based on the source and domain.
95+
$file = self::get_snippet_file($key, $source);
96+
97+
if (is_null($file)) {
98+
return;
99+
}
100+
101+
$headers = self::get_snippet_meta_from_file($file);
102+
103+
if (!array_key_exists('Snippet Title', $headers)) {
104+
return null;
37105
}
106+
107+
$snippet = new \stdClass();
108+
$snippet->title = $headers['Snippet Title'];
109+
$snippet->description = $headers['Description'];
110+
$snippet->scope = $headers['Scope'];
111+
$snippet->goal = $headers['Goal'];
112+
$snippet->source = 'theme_boost_union';
113+
114+
return $snippet;
38115
}
39116

40117
/**
@@ -45,14 +122,9 @@ public static function get_snippet($key, $domain = 'theme_boost_union') {
45122
public static function compose_snippets_data($snippetrecordset) {
46123
$snippets = [];
47124
foreach ($snippetrecordset as $snippetrecord) {
48-
if ('code' === $snippetrecord->source) {
49-
$snippetcontent = self::get_snippet($snippetrecord->key, $snippetrecord->domain);
50-
$snippetrecord->title = $snippetcontent['title'];
51-
$snippetrecord->description = $snippetcontent['description'];
52-
$snippetrecord->css = $snippetcontent['css'];
53-
$snippetrecord->goal = $snippetcontent['goal'];
54-
$snippetrecord->scope = $snippetcontent['scope'];
55-
$snippets[] = $snippetrecord;
125+
$snippet = self::get_snippet_meta($snippetrecord->key, $snippetrecord->source);
126+
if ($snippet) {
127+
$snippets[] = (object) array_merge((array) $snippetrecord, (array) $snippet);
56128
}
57129
}
58130
return $snippets;
@@ -76,14 +148,75 @@ public static function get_enabled_snippet_css() {
76148

77149
$css = '';
78150

79-
foreach ($data as $snippet) {
80-
if ($snippet->enabled) {
81-
$css .= self::get_snippet($snippet->key, $snippet->domain)['css'] . ' ';
82-
}
151+
// foreach ($data as $snippet) {
152+
// if ($snippet->enabled) {
153+
// $css .= self::get_snippet($snippet->key, $snippet->domain)['css'] . ' ';
154+
// }
155+
// }
156+
157+
return $css;
158+
}
159+
160+
/**
161+
* Strips close comment and close php tags from file headers.
162+
*
163+
* @param string $str Header comment to clean up.
164+
* @return string
165+
*/
166+
private static function cleanup_header_comment($str) {
167+
return trim(preg_replace('/\s*(?:\*\/|\?>).*/', '', $str));
168+
}
169+
170+
/**
171+
* Retrieves metadata from a file.
172+
*
173+
* Searches for metadata in the first 8 KB of a file, such as a plugin or theme.
174+
* Each piece of metadata must be on its own line. Fields can not span multiple
175+
* lines, the value will get cut at the end of the first line.
176+
*
177+
* If the file data is not within that first 8 KB, then the author should correct
178+
* the snippet.
179+
*
180+
* @param string $file Absolute path to the file.
181+
*
182+
* @return string[] Array of file header values keyed by header name.
183+
*/
184+
public static function get_snippet_meta_from_file($file,) {
185+
// Pull only the first 8 KB of the file in.
186+
$filedata = file_get_contents( $file, false, null, 0, 8 * self::KB_IN_BYTES );
187+
188+
if ( false === $filedata ) {
189+
$filedata = '';
83190
}
84191

85-
$data->close();
192+
// Make sure we catch CR-only line endings.
193+
$filedata = str_replace( "\r", "\n", $filedata );
86194

87-
return $css;
195+
$headers = [];
196+
197+
foreach (self::SNIPPET_HEADERS as $regex) {
198+
if (preg_match('/^(?:[ \t]*)?[ \t\/*#@]*' . preg_quote($regex, '/') . ':(.*)$/mi', $filedata, $match)
199+
&& $match[1]) {
200+
$headers[$regex] = self::cleanup_header_comment($match[1]);
201+
} else {
202+
$headers[$regex] = '';
203+
}
204+
}
205+
206+
return $headers;
88207
}
208+
209+
// /**
210+
// * Checks if there are any snippets not tracked in the database.
211+
// *
212+
// * If they are not they will be added to the database.
213+
// *
214+
// * @param moodle_recordset $data The snippets dataset currently present in the DB.
215+
// *
216+
// * @return void
217+
// */
218+
// public static function check_for_missing_snippets_int_the_database($data) {
219+
220+
// }
221+
89222
}

classes/table/snippets_overview.php

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ public function query_db($pagesize, $useinitialsbar = true) {
234234
// Get records.
235235
$data = $DB->get_recordset_sql($sql);
236236

237+
// snippets::check_for_missing_snippets_int_the_database($data);
238+
237239
$this->rawdata = snippets::compose_snippets_data($data);
238240
}
239241

snippets/builtin/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
To add a new snippet create a new css file containing the
2+
3+
/**
4+
* Snippet Title: Fix Borders
5+
* Domain: theme_boost_union
6+
* Scope: global
7+
* Goal: bugfix
8+
* Description: Those borders are annoying!
9+
*
10+
* @package theme_boost_union
11+
* @copyright 2024 University of Graz
12+
* @author André Menrath <[email protected]>
13+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
14+
*/
15+
16+
h1 {
17+
font-size: 70px;
18+
}

snippets/builtin/bigger_title.php

-34
This file was deleted.

snippets/builtin/fix_borders.php

-34
This file was deleted.

snippets/builtin/fix_font.php

-34
This file was deleted.

0 commit comments

Comments
 (0)