Created
January 30, 2012 01:52
-
-
Save joshtronic/1701900 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- My Script | |
- [IF] Check if file exists | |
- Reads the file in | |
- [FOREACH] Loops through the data | |
- [IF] Check if the data is empty | |
- Processes the data | |
- [ELSE] | |
- Does some other stuff | |
- Closes the file | |
- [WHILE] Just a while loop | |
- Increment counter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* My Script | |
*/ | |
// Check if file exists | |
if ($var == true) | |
{ | |
} | |
// Reads the file in | |
// Loops through the data | |
foreach ($array as $key => $value) | |
{ | |
// Check if the data is empty | |
if ($var == true) | |
{ | |
// Processes the data | |
} | |
else | |
{ | |
// Does some other stuff | |
} | |
} | |
// Closes the file | |
// Just a while loop | |
while ($i < 10) | |
{ | |
// Increment counter | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<body> | |
<h1>WorkFlowy Code Generator</h1> | |
<form action="" method="post"> | |
<textarea name="list" rows="20" cols="100"><?php | |
if (isset($_POST['list'])) | |
{ | |
$items = explode("\n", $_POST['list']); | |
if (count($items) > 0) | |
{ | |
$indents = null; | |
$previous_spaces = null; | |
$parenthesis = 0; | |
foreach ($items as $item) | |
{ | |
preg_match('/^( +)?(- )(.+)$/', $item, $matches); | |
$spaces = strlen($matches[1]); | |
$item = trim($matches[3]); | |
$is_syntax = (strpos($item, '[') === 0); | |
if ($item != '') | |
{ | |
if ($indents === null) | |
{ | |
$indents = 0; | |
echo '<?php' . "\n\n"; | |
echo '/**' . "\n" . ' * ' . $item . "\n" . ' */' . "\n\n"; | |
} | |
else | |
{ | |
if ($previous_spaces !== null) | |
{ | |
if ($previous_spaces > $spaces) | |
{ | |
$difference = $previous_spaces - $spaces; | |
for ($i = 0; $i < $difference; $i = $i + 2) | |
{ | |
$indents--; | |
if ($parenthesis > 0) | |
{ | |
echo str_repeat("\t", $indents) . '}' . "\n\n"; | |
$parenthesis--; | |
} | |
} | |
} | |
elseif ($previous_spaces < $spaces) | |
{ | |
$difference = $spaces - $previous_spaces; | |
for ($i = 0; $i < $difference; $i = $i + 2) | |
{ | |
$indents++; | |
} | |
} | |
else | |
{ | |
if ($parenthesis > 0) | |
{ | |
echo $indent_string . '}' . "\n\n"; | |
$parenthesis--; | |
} | |
} | |
} | |
$indent_string = str_repeat("\t", $indents); | |
if ($is_syntax) | |
{ | |
preg_match('/^(\[.+\])(.+)?$/', $item, $matches); | |
$type = substr($matches[1], 1, strlen($matches[1]) - 2); | |
$item = trim($matches[2]); | |
} | |
else | |
{ | |
$type = false; | |
} | |
if ($item != '') | |
{ | |
echo $indent_string . '// ' . $item . "\n"; | |
} | |
if ($type != false) | |
{ | |
switch ($type) | |
{ | |
case 'FOR': | |
echo $indent_string . 'for ($i = 0; $i < 10; $i++)' . "\n" . $indent_string . '{'; | |
break; | |
case 'FOREACH': | |
echo $indent_string . 'foreach ($array as $key => $value)' . "\n" . $indent_string . '{'; | |
break; | |
case 'IF': | |
echo $indent_string . 'if ($var == true)' . "\n" . $indent_string . '{'; | |
break; | |
case 'ELSEIF': | |
echo $indent_string . 'elseif ($var == false)' . "\n" . $indent_string . '{'; | |
break; | |
case 'ELSE': | |
echo $indent_string . 'else' . "\n" . $indent_string . '{'; | |
break; | |
case 'WHILE': | |
echo $indent_string . 'while ($i < 10)' . "\n" . $indent_string . '{'; | |
break; | |
} | |
$parenthesis++; | |
} | |
echo "\n"; | |
$previous_spaces = $spaces; | |
} | |
} | |
} | |
if ($parenthesis > 0) | |
{ | |
echo '}' . "\n\n"; | |
} | |
echo '?>'; | |
} | |
} | |
?></textarea> | |
<input type="submit" /> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment