Copie de Tumblr sur Shaarli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.1 KiB

  1. <?php
  2. namespace League\HTMLToMarkdown;
  3. use League\HTMLToMarkdown\Converter\BlockquoteConverter;
  4. use League\HTMLToMarkdown\Converter\CommentConverter;
  5. use League\HTMLToMarkdown\Converter\ConverterInterface;
  6. use League\HTMLToMarkdown\Converter\DefaultConverter;
  7. use League\HTMLToMarkdown\Converter\DivConverter;
  8. use League\HTMLToMarkdown\Converter\EmphasisConverter;
  9. use League\HTMLToMarkdown\Converter\HardBreakConverter;
  10. use League\HTMLToMarkdown\Converter\HeaderConverter;
  11. use League\HTMLToMarkdown\Converter\HorizontalRuleConverter;
  12. use League\HTMLToMarkdown\Converter\ImageConverter;
  13. use League\HTMLToMarkdown\Converter\LinkConverter;
  14. use League\HTMLToMarkdown\Converter\ListBlockConverter;
  15. use League\HTMLToMarkdown\Converter\ListItemConverter;
  16. use League\HTMLToMarkdown\Converter\ParagraphConverter;
  17. use League\HTMLToMarkdown\Converter\PreformattedConverter;
  18. use League\HTMLToMarkdown\Converter\TextConverter;
  19. final class Environment
  20. {
  21. /**
  22. * @var Configuration
  23. */
  24. protected $config;
  25. /**
  26. * @var ConverterInterface[]
  27. */
  28. protected $converters = array();
  29. public function __construct(array $config = array())
  30. {
  31. $this->config = new Configuration($config);
  32. $this->addConverter(new DefaultConverter());
  33. }
  34. /**
  35. * @return Configuration
  36. */
  37. public function getConfig()
  38. {
  39. return $this->config;
  40. }
  41. /**
  42. * @param ConverterInterface $converter
  43. */
  44. public function addConverter(ConverterInterface $converter)
  45. {
  46. if ($converter instanceof ConfigurationAwareInterface) {
  47. $converter->setConfig($this->config);
  48. }
  49. foreach ($converter->getSupportedTags() as $tag) {
  50. $this->converters[$tag] = $converter;
  51. }
  52. }
  53. /**
  54. * @param string $tag
  55. *
  56. * @return ConverterInterface
  57. */
  58. public function getConverterByTag($tag)
  59. {
  60. if (isset($this->converters[$tag])) {
  61. return $this->converters[$tag];
  62. }
  63. return $this->converters[DefaultConverter::DEFAULT_CONVERTER];
  64. }
  65. /**
  66. * @param array $config
  67. *
  68. * @return Environment
  69. */
  70. public static function createDefaultEnvironment(array $config = array())
  71. {
  72. $environment = new static($config);
  73. $environment->addConverter(new BlockquoteConverter());
  74. $environment->addConverter(new CommentConverter());
  75. $environment->addConverter(new DivConverter());
  76. $environment->addConverter(new EmphasisConverter());
  77. $environment->addConverter(new HardBreakConverter());
  78. $environment->addConverter(new HeaderConverter());
  79. $environment->addConverter(new HorizontalRuleConverter());
  80. $environment->addConverter(new ImageConverter());
  81. $environment->addConverter(new LinkConverter());
  82. $environment->addConverter(new ListBlockConverter());
  83. $environment->addConverter(new ListItemConverter());
  84. $environment->addConverter(new ParagraphConverter());
  85. $environment->addConverter(new PreformattedConverter());
  86. $environment->addConverter(new TextConverter());
  87. return $environment;
  88. }
  89. }