Skip to content

Commit

Permalink
Improvements to forms and YAML parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Gabryjeluk committed Jul 30, 2009
1 parent 17fff1e commit 2665aba
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Expand Up @@ -7,3 +7,6 @@
[submodule "lib/h2o"]
path = lib/h2o
url = git://github.com/speedmax/h2o-php.git
[submodule "lib/sfyaml"]
path = lib/sfyaml
url = git://github.com/fabpot/yaml.git
4 changes: 3 additions & 1 deletion lib/Text_Wiki/Text/Wiki/Parse/Default/Form.php
Expand Up @@ -66,7 +66,9 @@ function process(&$matches) {
$formYaml = $matches[1];
$dataYaml = $matches[2];

$json = new JSONService(SERVICES_JSON_LOOSE_TYPE);
if (substr($dataYaml, 0, 2) == '%%') {
$dataYaml = '';
}
$form = Wikidot_Form::fromYaml($formYaml, $dataYaml);

$output = $this->wiki->addToken($this->rule, array('begin' => 1));
Expand Down
1 change: 1 addition & 0 deletions lib/sfyaml
Submodule sfyaml added at f3abfa
2 changes: 1 addition & 1 deletion php/actions/WikiPageAction.php
Expand Up @@ -44,7 +44,7 @@ public function savePageEvent($runData){
$data[$m[1]] = $val;
}
}
$source = substr(Wikidot_Yaml::dump($data), 4);
$source = Wikidot_Yaml::dump($data);
} else {
$source = trim($pl->getParameterValue("source"));
}
Expand Down
4 changes: 2 additions & 2 deletions php/class/Wikidot/Form.php
Expand Up @@ -9,7 +9,7 @@ class Wikidot_Form {

public static function fromYaml($yamlString, $dataYamlString = null) {
$form = new self();
$yaml = Wikidot_Yaml::load($yamlString);
$yaml = Wikidot_Yaml::load($yamlString, true); // forgiving mode ;)

# relation between data type and field type
$datatypes = array('text' => 'wiki', 'page' => 'wiki');
Expand Down Expand Up @@ -90,7 +90,7 @@ public static function fromYaml($yamlString, $dataYamlString = null) {
public function setDataFromYaml($dataYamlString) {

if ($dataYamlString) {
$data = Wikidot_Yaml::load($dataYamlString);
$data = Wikidot_Yaml::load($dataYamlString, true); // forgiving mode again ;)
} else {
$data = array();
}
Expand Down
33 changes: 29 additions & 4 deletions php/class/Wikidot/Yaml.php
@@ -1,12 +1,37 @@
<?php

require_once(WIKIDOT_ROOT . '/lib/spyc/spyc.php');
require_once(WIKIDOT_ROOT . "/lib/sfyaml/lib/sfYamlParser.php");
require_once(WIKIDOT_ROOT . "/lib/sfyaml/lib/sfYaml.php");
require_once(WIKIDOT_ROOT . "/lib/spyc/spyc.php");

class Wikidot_Yaml {
public static function load($string) {
return Spyc::YAMLLoadString($string);
public static function load($string, $forgiving = false) {
if (substr($string, 0, 3) != '---') {
$string = "---\n$string";
}
try {
// if syck is available use it
if (extension_loaded('syck')) {
return syck_load($string);
}
// if not, use the symfony YAML parser
$yaml = new sfYamlParser();
return $yaml->parse($string);
} catch (Exception $e) {
if ($forgiving) {
// if YAML document is not correct,
// but we're forgiving, use the Spyc parser
return Spyc::YAMLLoadString($string);
}
throw new Wikidot_Yaml_Exception("Can't parse the YAML string." . $e->getMessage());
}
}
public static function dump($object) {
return Spyc::YAMLDump($object);
// using the slow (but very compatible) symfony YAML dumper
$ret = sfYaml::dump($object, 999);
if (substr($string, 0, 3) == '---') {
return substr($ret, 4);
}
return $ret;
}
}

0 comments on commit 2665aba

Please sign in to comment.