Skip to content
Browse files

Patterns parsers can dump comments and append to a text (not the PHP …

…one)
  • Loading branch information...
1 parent 4b853f4 commit af00744b32bc401da4748bf13b6b5951f3cb79b8 @shakty committed
View
10 includes/forms/lab.inc
@@ -237,13 +237,11 @@ function patterns_lab_submit($form, &$form_state) {
}
return;
}
-
- // Removed for now
- // Add an header to the exported pattern
- // $file = "# Patterns exported: " . $count . " \n";
- // $file .= "# Date and time: " . date('r') . "\n\n";
- //debug($export);
+
$file = patterns_parser_dump($export, $format);
+ // Add info to the exported pattern
+ $file = patterns_parser_dump_comment("Patterns exported: " . $count, $format, $file);
+ $file = patterns_parser_dump_comment("Date and time: " . date('r'), $format, $file);
if ($form_state['values']['to'] === PATTERNS_EXPORT_TO_DB) {
patterns_io_save_pattern($file, $filename, $format, $path);
View
35 includes/parser/parser.inc
@@ -171,13 +171,14 @@ function patterns_parser_parse($pattern, $format = PATTERNS_FORMAT_UNKNOWN) {
* @param mixed $pattern A string representation of the pattern, or a pattern
* array. In the latter case, the array is returned as it is, and the @param
* $format is ignored.
- *
* @param mixed $format The format against which parse the pattern string.
+ * @param mixed $append (optional) A string to which the dump will be
+ * appended. Defaults NULL
*
* @return bool|mixed The string representation of the pattern or FALSE.
*
*/
-function patterns_parser_dump($pattern, $format = PATTERNS_FORMAT_UNKNOWN) {
+function patterns_parser_dump($pattern, $format = PATTERNS_FORMAT_UNKNOWN, $append = NULL) {
if (empty($pattern)) return FALSE;
if (is_string($pattern)) return $pattern;
if (!is_array($pattern)) return FALSE;
@@ -187,7 +188,35 @@ function patterns_parser_dump($pattern, $format = PATTERNS_FORMAT_UNKNOWN) {
return FALSE;
}
- return $dump_function($pattern);
+ return $dump_function($pattern, $append);
+}
+
+/**
+ * Tries to dump an array representing a pattern to a string, according to
+ * the specified format.
+ *
+ * If succesfull returns the array representation of the pattern, if not
+ * returns FALSE;
+ *
+ * @param mixed $pattern A string representation of the pattern, or a pattern
+ * array. In the latter case, the array is returned as it is, and the @param
+ * $format is ignored.
+ * @param mixed $format The format against which parse the pattern string.
+ * @param mixed $append (optional) A string to which the dump will be
+ * appended. Defaults NULL
+ *
+ * @return bool|mixed The string representation of the pattern or FALSE.
+ *
+ */
+function patterns_parser_dump_comment($text, $format = PATTERNS_FORMAT_UNKNOWN, $append = NULL) {
+ if (empty($text)) return FALSE;
+
+ $dump_function = patterns_parser_get_parser_function($format, PATTERNS_PARSER_DUMP_COMMENT);
+ if (!$dump_function) {
+ return FALSE;
+ }
+
+ return $dump_function($text, $append);
}
/**
View
1 includes/variables.inc
@@ -16,6 +16,7 @@ define('PATTERNS_FIRST_INSTALL', 'patterns_first_install');
define('PATTERNS_PARSER_PARSE', 'parse');
define('PATTERNS_PARSER_DUMP', 'dump');
+define('PATTERNS_PARSER_DUMP_COMMENT', 'dump_comment');
define('PATTERNS_PARSER_LOAD', 'load');
define('PATTERNS_FORMAT_UNKNOWN', 'unknown');
View
29 patterns_phpparser/patterns_phpparser.module
@@ -68,14 +68,6 @@ function patterns_phpparser_parse($pattern = NULL) {
if (!is_string($pattern)) return FALSE;
return unserialize($pattern);
-
- // TODO: at the moment string parsing does not work
- //return FALSE;
-
- //$outout = array();
- //parse_str($pattern, $output);
- //$output = eval($pattern);
- //return $output;
}
/**
@@ -83,14 +75,19 @@ function patterns_phpparser_parse($pattern = NULL) {
*
* @param array $pattern The pattern to convert into a YAML string
*/
-function patterns_phpparser_dump($pattern = NULL) {
+function patterns_phpparser_dump($pattern = NULL, $append = NULL) {
if (is_null($pattern) || !is_array($pattern)) return FALSE;
- $string = serialize($pattern);
-
- //$string = '';
- //foreach ($subs as $key => $value) {
- // $string = "\$pattern = " . var_export($pattern, TRUE) . ";\n";
- //}
- return $string;
+ $dump = serialize($pattern);
+ return (empty($append)) ? $dump : $append . "\n" . $dump;
+}
+
+/**
+ * Comments are not allowed in the PHP format (for now).
+ *
+ * All the comments are removed.
+ *
+ */
+function patterns_phpparser_dump_comment($text = NULL, $append = NULL) {
+ return (is_null($append)) ? '' : $append;
}
View
28 patterns_xmlparser/patterns_xmlparser.module
@@ -64,8 +64,8 @@ function patterns_xmlparser_parse($xml) {
return $pattern;
}
-function patterns_xmlparser_dump($pattern = NULL) {
- if (is_null($pattern) || empty($pattern)) return FALSE;
+function patterns_xmlparser_dump($pattern = NULL, $append = NULL) {
+ if (empty($pattern)) return FALSE;
$xml = new SimpleXMLElement("<pattern/>");
@@ -76,9 +76,19 @@ function patterns_xmlparser_dump($pattern = NULL) {
$doc = dom_import_simplexml($xml)->ownerDocument;
$doc->preserveWhiteSpace = false;
$doc->formatOutput = true;
- return $doc->saveXML();
+
+ $dump = $doc->saveXML();
+
+ return (is_null($append)) ? $dump : $append . "\n" . $dump;
+}
+
+function patterns_xmlparser_dump_comment($text = NULL, $append = NULL) {
+ $text = (!empty($text)) ? $text : ' ';
+ $dump = '<!-- ' . $text . ' -->';
+ return (empty($append)) ? $dump : $append . "\n" . $dump;
}
+
// Helping Functions
// function defination to convert array to xml
@@ -160,11 +170,17 @@ function _patterns_xmlparser_process_action($action = NULL) {
if ($_action == PATTERNS_INCLUDE) {
// If the pattern is included by reference add it
// immediately, otherwise reparse the whole included pattern
- $inclusion = $action[0]['value'];
- if (is_array($inclusion)) {
+
+ //$inclusion = $action[0]['value'];
+ //if (is_array($inclusion)) {
+ if (!isset($action[0]['value'])) {
$inclusion = patterns_xmlparser_rearrange_data($action[0]);
}
- $_action_data = array('pattern' => $inclusion);
+ else {
+ $inclusion = $action[0]['value'];
+ }
+ $_action_data = array('pattern' => $inclusion);
+
}
else {
$_action_data = _patterns_xmlparser_rearrange_data($action);
View
23 patterns_yamlparser/patterns_yamlparser.module
@@ -49,11 +49,24 @@ function patterns_yamlparser_parse($pattern) {
* Converts an array representing a pattern into a YAML string.
*
* @param array $pattern The pattern to convert into a YAML string
+ * @param mixed $append (optional) A string to which the dump
+ * will be appended. Defaults NULL
+ *
+ * @return mixed $dump A string representation of the pattern.
*/
-function patterns_yamlparser_dump($pattern = NULL) {
-if (is_null($pattern) || !is_array($pattern)) return FALSE;
-
+function patterns_yamlparser_dump($pattern = NULL, $append = NULL) {
+ if (is_null($pattern) || !is_array($pattern)) return FALSE;
+ $dump = (empty($append)) ? '' : $append . "\n";
+
require_once libraries_get_path('spyc') . '/spyc.php';
- $pattern = Spyc::YAMLDump($pattern);
- return $pattern;
+ $dump.= Spyc::YAMLDump($pattern);
+ return $dump;
}
+
+function patterns_yamlparser_dump_comment($text = NULL, $append = NULL) {
+ if (is_null($text)) return FALSE;
+ $dump = (empty($append)) ? '' : $append . "\n";
+ $dump.= "# " . $text;
+ return $dump;
+}
+

0 comments on commit af00744

Please sign in to comment.
Something went wrong with that request. Please try again.