Skip to content

Commit

Permalink
- Clean up some redundant code for doing the indent.
Browse files Browse the repository at this point in the history
- Support for changing the indent character from tab to space.
- Support for specifying how many spaces to indent.
- Cli interface for the same.
  • Loading branch information
jongardiner committed May 11, 2017
1 parent 7c5b99b commit a0ea552
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 37 deletions.
9 changes: 6 additions & 3 deletions bin/twigify.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@

$fileName = "";

$converter = new Converter();
$indenter = new \BambooHR\Twigify\Indenter();
$converter = new Converter($indenter);
for($i=1;$i<count($_SERVER['argv']); ++$i) {
$arg = $_SERVER['argv'][$i];
if($arg=='-j' || $arg=='jinja') {
$converter = new JinjaConverter();
if($arg=='-j' || $arg=='--jinja') {
$converter = new JinjaConverter($indenter);
} else if($arg=='-s' && $i+1 < count($_SERVER['argv'])) {
$indenter->setIndent(" ", $_SERVER['argv'][++$i] );
} else {
$fileName = $arg;
}
Expand Down
37 changes: 15 additions & 22 deletions lib/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Converter extends \PhpParser\PrettyPrinter\Standard {
private $errors = [];
protected $indenter;

public function __construct(array $options = []) {
public function __construct(Indenter $indenter, array $options = []) {
parent::__construct($options);
$this->indenter=new Indenter();
$this->indenter = $indenter;
}


Expand Down Expand Up @@ -486,20 +486,15 @@ public function pStmt_Function(Stmt\Function_ $node) {
}
$ret.=")";
$this->indenter->statement($ret);
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts, true);
$this->indenter->statement("endmacro");
return $ret;
}

// Control flow

public function pStmt_If(Stmt\If_ $node) {
$this->indenter->statement("if " . $this->p($node->cond));
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts, true);
foreach($node->elseifs as $stmt) {
$this->p($stmt);
}
Expand All @@ -511,16 +506,12 @@ public function pStmt_If(Stmt\If_ $node) {

public function pStmt_ElseIf(Stmt\ElseIf_ $node) {
$this->indenter->statement('elseif ' . $this->p($node->cond) . ')' );
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts, true);
}

public function pStmt_Else(Stmt\Else_ $node) {
$this->indenter->statement("else");
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts, true);
}

private function getForMax(Stmt\For_ $node) {
Expand Down Expand Up @@ -598,13 +589,14 @@ public function pStmt_For(Stmt\For_ $node) {
$step = $this->getForStep($name, $node);

$this->indenter->statement("for $loopCounter in range($min, $max, $step)");
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts,true);
$this->indenter->statement("endfor");
}

protected function pStmts(array $nodes, $indent = true) {
if($indent) {
$this->indenter->indent();
}
foreach ($nodes as $node) {
$this->pComments($node->getAttribute('comments', array()));
if ($node instanceof Expr) {
Expand All @@ -619,6 +611,9 @@ protected function pStmts(array $nodes, $indent = true) {
$this->p($node);
}
}
if($indent) {
$this->indenter->unindent();
}
}

public function pStmt_Foreach(Stmt\Foreach_ $node) {
Expand All @@ -627,9 +622,7 @@ public function pStmt_Foreach(Stmt\Foreach_ $node) {
. $this->p($node->valueVar)
. " in ". $this->p($node->expr)
);
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts, true);
$this->indenter->statement("endfor");
}

Expand Down Expand Up @@ -718,7 +711,7 @@ protected function pComments(array $comments) {
}

public function prettyPrint(array $stmts) {
$this->pStmts( $stmts );
$this->pStmts( $stmts, false );
return $this->indenter->getOutput();
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/Indenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
namespace BambooHR\Twigify;

class Indenter {
const TAB = "\t";
private $indent = 0;
private $indentSequence = "\t";
private $inTag = false;
private $inCloseTag = false;

Expand All @@ -24,6 +24,10 @@ class Indenter {

private $output = "";

function setIndent($str, $count) {
$this->indentSequence = str_repeat($str, $count);
}

function indent() {
$this->indent++;
}
Expand All @@ -41,7 +45,7 @@ function append($str) {
}

function newLine() {
$this->output = rtrim($this->output)."\n". str_repeat(self::TAB, $this->indent);
$this->output = rtrim($this->output)."\n". str_repeat($this->indentSequence, $this->indent);
}

function statement($str) {
Expand Down
12 changes: 3 additions & 9 deletions lib/JinjaConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@
class JinjaConverter extends Converter
{
public function pStmt_ElseIf(Stmt\ElseIf_ $node) {
$this->indenter->statement("elif " . $this->p($node->cond) . ')' );
$this->indenter->indent();
$this->indenter->statement("elif " . $this->p($node->cond) . ')', true );
$this->pStmts($node->stmts);
$this->indenter->unindent();
}

public function pStmt_Foreach(Stmt\Foreach_ $node) {
Expand All @@ -26,19 +24,15 @@ public function pStmt_Foreach(Stmt\Foreach_ $node) {
. $this->p($node->valueVar)
. " in " . $this->p($node->expr) . '.iteritems()'
);
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts,true);
$this->indenter->statement('endfor');
} else {
$this->indenter->statement(
'for '
. $this->p($node->valueVar)
. " in " . $this->p($node->expr)
);
$this->indenter->indent();
$this->pStmts($node->stmts);
$this->indenter->unindent();
$this->pStmts($node->stmts,true);
}
}
}
2 changes: 1 addition & 1 deletion sample-files/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<html> <head>
<style>
.a { color: red}
.a { color: red; }

div {
margin: 5px;
Expand Down

0 comments on commit a0ea552

Please sign in to comment.