-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add file ability, php-cs-fixer, prepare changelog
- Loading branch information
Showing
3 changed files
with
68 additions
and
51 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,57 +7,71 @@ | |
|
||
/** | ||
* Fix Markdown Files. | ||
* | ||
* | ||
* | ||
* Provide Folder: | ||
* | ||
* ``` | ||
* ./vendor/bin/gmf fix /folder | ||
* ``` | ||
* | ||
* Provide File: | ||
* ``` | ||
* ./vendor/bin/gmf file /path/to/README.md | ||
* ``` | ||
* | ||
* @author Basil Suter <[email protected]> | ||
*/ | ||
class FixController extends Command | ||
{ | ||
/** | ||
* @var boolean Whether to run the command but doe not change the file content. | ||
*/ | ||
public $dry = false; | ||
/** | ||
* | ||
* {@inheritDoc} | ||
* @see \luya\console\Command::options() | ||
*/ | ||
public function options($actionID) | ||
{ | ||
return ['dry']; | ||
} | ||
/** | ||
* | ||
* {@inheritDoc} | ||
* @see \yii\console\Controller::optionAliases() | ||
*/ | ||
public function optionAliases() | ||
{ | ||
return ['d' => 'dry']; | ||
} | ||
/** | ||
* Fixing files inside a given Folder. | ||
* | ||
* @param string $folder The folder where the markdown files are located. | ||
* @return integer | ||
*/ | ||
/** | ||
* @var boolean Whether to run the command but doe not change the file content. | ||
*/ | ||
public $dry = false; | ||
/** | ||
* | ||
* {@inheritDoc} | ||
* @see \luya\console\Command::options() | ||
*/ | ||
public function options($actionID) | ||
{ | ||
return ['dry']; | ||
} | ||
/** | ||
* | ||
* {@inheritDoc} | ||
* @see \yii\console\Controller::optionAliases() | ||
*/ | ||
public function optionAliases() | ||
{ | ||
return ['d' => 'dry']; | ||
} | ||
/** | ||
* Fixing files inside a given Folder. | ||
* | ||
* @param string $folder The folder where the markdown files are located or a single file | ||
* @return integer | ||
*/ | ||
public function actionIndex($folder) | ||
{ | ||
$files = FileHelper::findFiles($folder, [ | ||
'recursive' => true, | ||
'caseSensitive' => false, | ||
'only'=> ['*.md'], | ||
]); | ||
if (is_file($folder)) { | ||
$files[] = $folder; | ||
} else { | ||
$files = FileHelper::findFiles($folder, [ | ||
'recursive' => true, | ||
'caseSensitive' => false, | ||
'only'=> ['*.md'], | ||
]); | ||
} | ||
|
||
foreach ($files as $file) { | ||
$content = $this->getFileContent($file); | ||
|
||
if (!$content) { | ||
$this->outputError("Unable to read file: " . $file); | ||
continue; | ||
$this->outputError("Unable to read file: " . $file); | ||
continue; | ||
} | ||
|
||
$newcontent = $this->parseContent($content); | ||
|
@@ -67,7 +81,7 @@ public function actionIndex($folder) | |
} | ||
|
||
if (!$this->dry) { | ||
file_put_contents($file, $newcontent); | ||
file_put_contents($file, $newcontent); | ||
} | ||
} | ||
|
||
|
@@ -81,18 +95,18 @@ public function actionIndex($folder) | |
*/ | ||
public function getFileContent($file) | ||
{ | ||
return FileHelper::getFileContent($file); | ||
return FileHelper::getFileContent($file); | ||
} | ||
|
||
/** | ||
* Parse Content of a File and replace if required. | ||
* | ||
* | ||
* @param string $content The content to fix | ||
* @return string The fixed content. | ||
*/ | ||
public function parseContent($content) | ||
{ | ||
// replace breaking spaces with spaces | ||
// replace breaking spaces with spaces | ||
$content = preg_replace('/xC2xA0/', ' ', $content); | ||
$content = preg_replace('~\x{00a0}~siu', ' ', $content); | ||
|
||
|
@@ -101,4 +115,4 @@ public function parseContent($content) | |
|
||
return $content; | ||
} | ||
} | ||
} |