public function testTitleTransformedWithBold()
 {
     $transformer = new Transformer();
     $json_file = file_get_contents(__DIR__ . '/wp-rules.json');
     $transformer->loadRules($json_file);
     $title_html_string = '<?xml encoding="utf-8" ?><h1>Title <b>in bold</b></h1>';
     libxml_use_internal_errors(true);
     $document = new \DOMDocument();
     $document->loadHtml($title_html_string);
     libxml_use_internal_errors(false);
     $header = Header::create();
     $transformer->transform($header, $document);
     $this->assertEquals('<h1>Title <b>in bold</b></h1>', $header->getTitle()->render());
 }
    public function testTransformPullquote()
    {
        $transformer_rules = <<<'JSON'
{
    "rules" : [
        {
            "class": "TextNodeRule"
        },
        {
            "class": "ItalicRule",
            "selector": "em"
        },
        {
            "class": "ParagraphRule",
            "selector": "p"
        },
        {
            "class": "PassThroughRule",
            "selector": "div.field-quote > p"
        },
        {
            "class": "PassThroughRule",
            "selector" : "div.field-quote"
        },
        {
            "class" : "PullquoteRule",
            "selector" : "blockquote.pull-quote"
        },
        {
            "class" : "PullquoteCiteRule",
            "selector" : "div.field-quote-author"
        }
    ]
}
JSON;
        $html = '<blockquote class="pull-quote">' . '<div class="field-quote">' . '<p>Here is a fancy pull quote for the <em>world</em> to see it all.</p>' . '</div>' . '<div class="field-quote-author">Matthew Oliveira</div>' . '</blockquote>';
        $expected = "<aside>Here is a fancy pull quote for the <i>world</i> to see it all." . "<cite>Matthew Oliveira</cite>" . "</aside>\n";
        $instant_article = InstantArticle::create();
        $transformer = new Transformer();
        $transformer->loadRules($transformer_rules);
        $document = new \DOMDocument();
        $document->loadXML($html);
        $transformer->transform($instant_article, $document);
        $pullquote = $instant_article->getChildren()[0];
        $result = $pullquote->render('', true) . "\n";
        $this->assertEquals($expected, $result);
    }
예제 #3
0
 /**
  * @param string|DOMDocument $document The document html of an Instant Article
  *
  * @return InstantArticle filled element that was parsed from the DOMDocument parameter
  */
 public function parse($content)
 {
     if (Type::is($content, Type::STRING)) {
         libxml_use_internal_errors(true);
         $document = new \DOMDocument();
         $document->loadHTML($content);
         libxml_use_internal_errors(false);
     } else {
         $document = $content;
     }
     $json_file = file_get_contents(__DIR__ . '/instant-articles-rules.json');
     $instant_article = InstantArticle::create();
     $transformer = new Transformer();
     $transformer->loadRules($json_file);
     $transformer->transform($instant_article, $document);
     return $instant_article;
 }
 public function testSelfTransformerContent()
 {
     $json_file = file_get_contents('src/Facebook/InstantArticles/Parser/instant-articles-rules.json');
     $instant_article = InstantArticle::create();
     $transformer = new Transformer();
     $transformer->loadRules($json_file);
     $html_file = file_get_contents(__DIR__ . '/instant-article-example.html');
     libxml_use_internal_errors(true);
     $document = new \DOMDocument();
     $document->loadHTML($html_file);
     libxml_use_internal_errors(false);
     $transformer->transform($instant_article, $document);
     $instant_article->addMetaProperty('op:generator:version', '1.0.0');
     $instant_article->addMetaProperty('op:generator:transformer:version', '1.0.0');
     $result = $instant_article->render('', true) . "\n";
     //var_dump($result);
     // print_r($warnings);
     $this->assertEquals($html_file, $result);
 }
 public function testTransformerCustomHTML()
 {
     $json_file = file_get_contents(__DIR__ . '/custom-html-rules.json');
     $instant_article = InstantArticle::create();
     $transformer = new Transformer();
     $transformer->loadRules($json_file);
     $html_file = file_get_contents(__DIR__ . '/custom.html');
     libxml_use_internal_errors(true);
     $document = new \DOMDocument();
     $document->loadHTML($html_file);
     libxml_use_internal_errors(false);
     $instant_article->withCanonicalURL('http://localhost/article')->withHeader(Header::create()->withTitle('Peace on <b>earth</b>')->addAuthor(Author::create()->withName('bill'))->withPublishTime(Time::create(Time::PUBLISHED)->withDatetime(\DateTime::createFromFormat('j-M-Y G:i:s', '12-Apr-2016 19:46:51'))));
     $transformer->transform($instant_article, $document);
     $instant_article->addMetaProperty('op:generator:version', '1.0.0');
     $instant_article->addMetaProperty('op:generator:transformer:version', '1.0.0');
     $result = $instant_article->render('', true) . "\n";
     $expected = file_get_contents(__DIR__ . '/custom-html-ia.xml');
     $this->assertEquals($expected, $result);
     // there must be 3 warnings related to <img> inside <li> that is not supported by IA
     $this->assertEquals(3, count($transformer->getWarnings()));
 }