Guest User

Untitled

a guest
Jan 22nd, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'phing/Task.php';
  4. require_once 'phing/util/FileUtils.php';
  5. require_once 'System.php';
  6. require_once "phing/tasks/ext/markdown/markdown.php";
  7.  
  8. class MarkdownTask extends Task {
  9.  
  10. /**
  11. * @var string Taskname for logger
  12. */
  13. protected $taskName = 'Markdown';
  14.  
  15. /**
  16. * Result format, defaults to "html".
  17. * @see $supportedFormats for all possible options
  18. *
  19. * @var string
  20. */
  21. protected $format = 'html';
  22.  
  23. /**
  24. * Input file in markdown format.
  25. * Required
  26. *
  27. * @var string
  28. */
  29. protected $file = null;
  30.  
  31. /**
  32. * Output file or directory. May be omitted.
  33. * When it ends with a slash, it is considered to be a directory
  34. *
  35. * @var string
  36. */
  37. protected $destination = null;
  38.  
  39. protected $removefilesetdir = null;
  40.  
  41. protected $filesets = array(); // all fileset objects assigned to this task
  42. protected $mapperElement = null;
  43.  
  44. /**
  45. * all filterchains objects assigned to this task
  46. *
  47. * @var array
  48. */
  49. protected $filterChains = array();
  50.  
  51. /**
  52. * mode to create directories with
  53. *
  54. * @var integer
  55. */
  56. protected $mode = 0755;
  57.  
  58. /**
  59. * Only render files whole source files are newer than the
  60. * target files
  61. *
  62. * @var boolean
  63. */
  64. protected $uptodate = false;
  65.  
  66. /**
  67. * The main entry point method.
  68. *
  69. * @return void
  70. */
  71. public function main()
  72. {
  73. if (count($this->filterChains)) {
  74. $this->fileUtils = new FileUtils();
  75. }
  76.  
  77. if ($this->file != '') {
  78. $file = $this->file;
  79. $targetFile = $this->getTargetFile($file, $this->destination);
  80. $this->render($file, $targetFile);
  81. return;
  82. }
  83.  
  84. if (!count($this->filesets)) {
  85. throw new BuildException(
  86. '"file" attribute or "fileset" subtag required'
  87. );
  88. }
  89.  
  90. // process filesets
  91. $mapper = null;
  92. if ($this->mapperElement !== null) {
  93. $mapper = $this->mapperElement->getImplementation();
  94. }
  95.  
  96. $project = $this->getProject();
  97. foreach ($this->filesets as $fs) {
  98. $ds = $fs->getDirectoryScanner($project);
  99. $fromDir = $fs->getDir($project);
  100. $srcFiles = $ds->getIncludedFiles();
  101.  
  102. foreach ($srcFiles as $src) {
  103. $file = new PhingFile($fromDir, $src);
  104. if ($mapper !== null) {
  105. $results = $mapper->main($file);
  106. if ($results === null) {
  107. throw new BuildException(
  108. sprintf(
  109. 'No filename mapper found for "%s"',
  110. $file
  111. )
  112. );
  113. }
  114. $targetFile = reset($results);
  115. } else {
  116. $targetFile = $this->getTargetFile($file, $this->destination);
  117. }
  118. $this->render($file, $targetFile);
  119. }
  120. }
  121. }
  122.  
  123. /**
  124. * Renders a single file and applies filters on it
  125. *
  126. * @param string $tool conversion tool to use
  127. * @param string $source markdown source file
  128. * @param string $targetFile target file name
  129. *
  130. * @return void
  131. */
  132. protected function render($source, $targetFile)
  133. {
  134. if (count($this->filterChains) == 0) {
  135. return $this->renderFile($source, $targetFile);
  136. }
  137.  
  138. $tmpTarget = tempnam(sys_get_temp_dir(), 'tmp-');
  139. $this->renderFile($source, $tmpTarget);
  140.  
  141. $this->fileUtils->copyFile(
  142. new PhingFile($tmpTarget),
  143. new PhingFile($targetFile),
  144. true, false, $this->filterChains,
  145. $this->getProject(), $this->mode
  146. );
  147. unlink($tmpTarget);
  148. }
  149.  
  150. /**
  151. * Renders a single file with the markdown tool.
  152. *
  153. * @param string $tool conversion tool to use
  154. * @param string $source markdown source file
  155. * @param string $targetFile target file name
  156. *
  157. * @return void
  158. *
  159. * @throws BuildException When the conversion fails
  160. */
  161. protected function renderFile($source, $targetFile)
  162. {
  163. if ($this->uptodate && file_exists($targetFile)
  164. && filemtime($source) <= filemtime($targetFile)
  165. ) {
  166. //target is up to date
  167. return;
  168. }
  169. //work around a bug in php by replacing /./ with /
  170. $targetDir = str_replace('/./', '/', dirname($targetFile));
  171. if (!is_dir($targetDir)) {
  172. mkdir($targetDir, $this->mode, true);
  173. }
  174.  
  175. $arOutput = Markdown(file_get_contents($source));
  176.  
  177. $retval = file_put_contents($targetFile,$arOutput);
  178.  
  179. if ( ! $retval) {
  180. $this->log('File not rendered.', Project::MSG_INFO);
  181. throw new BuildException('Rendering markdown failed');
  182. }
  183. $this->log('File rendered.', Project::MSG_DEBUG);
  184. }
  185.  
  186. /**
  187. * Determines and returns the target file name from the
  188. * input file and the configured destination name.
  189. *
  190. * @param string $file Input file
  191. * @param string $destination Destination file or directory name,
  192. * may be null
  193. *
  194. * @return string Target file name
  195. *
  196. * @uses $format
  197. * @uses $targetExt
  198. */
  199. public function getTargetFile($file, $destination = null)
  200. {
  201. if ($destination != ''
  202. && substr($destination, -1) !== '/'
  203. && substr($destination, -1) !== '\\'
  204. ) {
  205. return $destination;
  206. }
  207.  
  208. if (strtolower(substr($file, -3)) == '.md') {
  209. $file = substr($file, 0, -3);
  210. }
  211.  
  212. if($this->removefilesetdir)
  213. $file = str_replace($this->removefilesetdir.DIRECTORY_SEPARATOR,'',$file);
  214.  
  215. return $destination . $file . '.' . $this->format;
  216. }
  217.  
  218. /**
  219. * The setter for the attribute "file"
  220. *
  221. * @param string $file Path of file to render
  222. *
  223. * @return void
  224. */
  225. public function setFile($file)
  226. {
  227. $this->file = $file;
  228. }
  229.  
  230. /**
  231. * The setter for the attribute "destination"
  232. *
  233. * @param string $destination Output file or directory. When it ends
  234. * with a slash, it is taken as directory.
  235. *
  236. * @return void
  237. */
  238. public function setDestination($destination)
  239. {
  240. $this->destination = $destination;
  241. }
  242.  
  243. /**
  244. * The setter for the attribute "destination"
  245. *
  246. * @param string $destination Output file or directory. When it ends
  247. * with a slash, it is taken as directory.
  248. *
  249. * @return void
  250. */
  251. public function setRemovefilesetdir($removefilesetdir)
  252. {
  253. $this->removefilesetdir = $removefilesetdir;
  254. }
  255.  
  256. /**
  257. * The setter for the attribute "uptodate"
  258. *
  259. * @param string $uptodate True/false
  260. *
  261. * @return void
  262. */
  263. public function setUptodate($uptodate)
  264. {
  265. $this->uptodate = (boolean)$uptodate;
  266. }
  267.  
  268. /**
  269. * Nested creator, creates a FileSet for this task
  270. *
  271. * @return object The created fileset object
  272. */
  273. public function createFileSet()
  274. {
  275. $num = array_push($this->filesets, new FileSet());
  276. return $this->filesets[$num-1];
  277. }
  278.  
  279. /**
  280. * Nested creator, creates one Mapper for this task
  281. *
  282. * @return Mapper The created Mapper type object
  283. *
  284. * @throws BuildException
  285. */
  286. public function createMapper()
  287. {
  288. if ($this->mapperElement !== null) {
  289. throw new BuildException(
  290. 'Cannot define more than one mapper', $this->location
  291. );
  292. }
  293. $this->mapperElement = new Mapper($this->project);
  294. return $this->mapperElement;
  295. }
  296.  
  297. /**
  298. * Creates a filterchain, stores and returns it
  299. *
  300. * @return FilterChain The created filterchain object
  301. */
  302. public function createFilterChain()
  303. {
  304. $num = array_push($this->filterChains, new FilterChain($this->project));
  305. return $this->filterChains[$num-1];
  306. }
  307. }
Add Comment
Please, Sign In to add comment