MarkdownFormatter.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @package Grav\Framework\File\Formatter
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\File\Formatter;
  9. class MarkdownFormatter implements FormatterInterface
  10. {
  11. /** @var array */
  12. private $config;
  13. /** @var FormatterInterface */
  14. private $headerFormatter;
  15. public function __construct(array $config = [], FormatterInterface $headerFormatter = null)
  16. {
  17. $this->config = $config + [
  18. 'file_extension' => '.md',
  19. 'header' => 'header',
  20. 'body' => 'markdown',
  21. 'raw' => 'frontmatter',
  22. 'yaml' => ['inline' => 20]
  23. ];
  24. $this->headerFormatter = $headerFormatter ?: new YamlFormatter($this->config['yaml']);
  25. }
  26. /**
  27. * @deprecated 1.5 Use $formatter->getDefaultFileExtension() instead.
  28. */
  29. public function getFileExtension()
  30. {
  31. return $this->getDefaultFileExtension();
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function getDefaultFileExtension()
  37. {
  38. $extensions = $this->getSupportedFileExtensions();
  39. return (string) reset($extensions);
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function getSupportedFileExtensions()
  45. {
  46. return (array) $this->config['file_extension'];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function encode($data)
  52. {
  53. $headerVar = $this->config['header'];
  54. $bodyVar = $this->config['body'];
  55. $header = isset($data[$headerVar]) ? (array) $data[$headerVar] : [];
  56. $body = isset($data[$bodyVar]) ? (string) $data[$bodyVar] : '';
  57. // Create Markdown file with YAML header.
  58. $encoded = '';
  59. if ($header) {
  60. $encoded = "---\n" . trim($this->headerFormatter->encode($data['header'])) . "\n---\n\n";
  61. }
  62. $encoded .= $body;
  63. // Normalize line endings to Unix style.
  64. $encoded = preg_replace("/(\r\n|\r)/", "\n", $encoded);
  65. return $encoded;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function decode($data)
  71. {
  72. $headerVar = $this->config['header'];
  73. $bodyVar = $this->config['body'];
  74. $rawVar = $this->config['raw'];
  75. $content = [
  76. $headerVar => [],
  77. $bodyVar => ''
  78. ];
  79. $headerRegex = "/^---\n(.+?)\n---\n{0,}(.*)$/uis";
  80. // Normalize line endings to Unix style.
  81. $data = preg_replace("/(\r\n|\r)/", "\n", $data);
  82. // Parse header.
  83. preg_match($headerRegex, ltrim($data), $matches);
  84. if(empty($matches)) {
  85. $content[$bodyVar] = $data;
  86. } else {
  87. // Normalize frontmatter.
  88. $frontmatter = preg_replace("/\n\t/", "\n ", $matches[1]);
  89. if ($rawVar) {
  90. $content[$rawVar] = $frontmatter;
  91. }
  92. $content[$headerVar] = $this->headerFormatter->decode($frontmatter);
  93. $content[$bodyVar] = $matches[2];
  94. }
  95. return $content;
  96. }
  97. }