Return longest text after failing to detect text longer than the configured value (#423)

Save extracted text across attempts and return the longest one when all attempts fail, and add a test case from hukumusume
pull/394/merge
Andres Rey 6 years ago committed by Gijs
parent 264b8e8968
commit 834672ef86

@ -34,6 +34,7 @@ function Readability(uri, doc, options) {
this._articleTitle = null;
this._articleByline = null;
this._articleDir = null;
this._attempts = [];
// Configurable options
this._debug = !!options.debug;
@ -1083,24 +1084,45 @@ Readability.prototype = {
if (this._debug)
this.log("Article content after paging: " + articleContent.innerHTML);
var parseSuccessful = true;
// Now that we've gone through the full algorithm, check to see if
// we got any meaningful content. If we didn't, we may need to re-run
// grabArticle with different flags set. This gives us a higher likelihood of
// finding the content, and the sieve approach gives us a higher likelihood of
// finding the -right- content.
if (this._getInnerText(articleContent, true).length < this._wordThreshold) {
var textLength = this._getInnerText(articleContent, true).length;
if (textLength < this._wordThreshold) {
parseSuccessful = false;
page.innerHTML = pageCacheHtml;
if (this._flagIsActive(this.FLAG_STRIP_UNLIKELYS)) {
this._removeFlag(this.FLAG_STRIP_UNLIKELYS);
this._attempts.push({articleContent: articleContent, textLength: textLength});
} else if (this._flagIsActive(this.FLAG_WEIGHT_CLASSES)) {
this._removeFlag(this.FLAG_WEIGHT_CLASSES);
this._attempts.push({articleContent: articleContent, textLength: textLength});
} else if (this._flagIsActive(this.FLAG_CLEAN_CONDITIONALLY)) {
this._removeFlag(this.FLAG_CLEAN_CONDITIONALLY);
this._attempts.push({articleContent: articleContent, textLength: textLength});
} else {
return null;
this._attempts.push({articleContent: articleContent, textLength: textLength});
// No luck after removing flags, just return the longest text we found during the different loops
this._attempts.sort(function (a, b) {
return a.textLength < b.textLength;
});
// But first check if we actually have something
if (!this._attempts[0].textLength) {
return null;
}
articleContent = this._attempts[0].articleContent;
parseSuccessful = true;
}
} else {
}
if (parseSuccessful) {
// Find out text direction from ancestors of final top candidate.
var ancestors = [parentOfTopCandidate, topCandidate].concat(this._getNodeAncestors(parentOfTopCandidate));
this._someNode(ancestors, function(ancestor) {

@ -0,0 +1,7 @@
{
"title": "欲張りなイヌ <福娘童話集 きょうのイソップ童話>",
"byline": null,
"dir": null,
"excerpt": "福娘童話集 > きょうのイソップ童話 > 1月のイソップ童話 > 欲張りなイヌ",
"readerable": true
}

@ -0,0 +1,57 @@
<div id="readability-page-1" class="page"><div width="619">
<p> <a href="http://fakehost/test/../../../index.html">福娘童話集</a> > <a href="http://fakehost/test/../index.html">きょうのイソップ童話</a> &gt; <a href="http://fakehost/test/../itiran/01gatu.htm">1月のイソップ童話</a> &gt; 欲張りなイヌ</p>
<p>
<span color="#FF0000" size="+2">元旦のイソップ童話</span></p><p> <img src="http://fakehost/test/../../../gazou/pc_gazou/aesop/aesop052.jpg" alt="よくばりなイヌ" width="480" height="360"></img></p><p> 欲張りなイヌ</p><p> <a href="http://hukumusume.com/douwa/English/aesop/01/01_j.html">ひらがな</a> ←→ <a href="http://hukumusume.com/douwa/English/aesop/01/01_j&amp;E.html">日本語・英語</a> ←→ <a href="http://hukumusume.com/douwa/English/aesop/01/01_E.html">English</a></p>
<table>
<tbody>
<tr>
<td>
<table>
<tbody>
<tr>
<td><img src="http://fakehost/test/../../../../366/logo_bana/corner_1.gif" width="7" height="7"></img></td>
<td>
<span color="#FF0000"><b>おりがみをつくろう</b></span>
</td>
<td>
<span size="-1">(<a href="http://www.origami-club.com/index.html"> おりがみくらぶ</a> より)</span>
</td>
<td><img src="http://fakehost/test/../../../../366/logo_bana/corner_2.gif" width="7" height="7"></img></td>
</tr>
<tr>
<td colspan="4">
<table>
<tbody>
<tr>
<td>
<span size="+2"><a href="http://www.origami-club.com/easy/dogfase/index.html"><img src="http://fakehost/test/../../../gazou/origami_gazou/kantan/dogface.gif" alt="犬の顔の折り紙" width="73" height="51"></img>いぬのかお</a></span>   <a href="http://www.origami-club.com/easy/dog/index.html"><img src="http://fakehost/test/../../../gazou/origami_gazou/kantan/dog.gif" alt="犬の顔の紙" width="62" height="43"></img><span size="+2">いぬ</span></a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table>
<tbody>
<tr>
<td>♪音声配信(html5)</td>
</tr>
<tr>
<td><audio src="http://ohanashi2.up.seesaa.net/mp3/ae_0101.mp3" controls=""></audio></td>
</tr>
<tr>
<td>
<span size="-1"><a href="http://www.voiceblog.jp/onokuboaki/">亜姫の朗読☆ イソップ童話より</a></span>
</td>
</tr>
</tbody>
</table>
<p>  肉をくわえたイヌが、橋を渡っていました。 ふと下を見ると、川の中にも肉をくわえたイヌがいます。 イヌはそれを見て、思いました。(あいつの肉の方が、大きそうだ)  イヌは、くやしくてたまりません。 (そうだ、あいつをおどかして、あの肉を取ってやろう)  そこでイヌは、川の中のイヌに向かって思いっきり吠えました。 「ウゥー、ワン!!」  そのとたん、くわえていた肉はポチャンと川の中に落ちてしまいました。 「ああー、ぁぁー」  川の中には、がっかりしたイヌの顔がうつっています。  さっきの川の中のイヌは、水にうつった自分の顔だったのです。  同じ物を持っていても、人が持っている物の方が良く見え、また、欲張るとけっきょく損をするというお話しです。</p>
<p>おしまい</p>
<p> </p>
</div></div>

@ -0,0 +1,273 @@
<html>
<head>
<title>欲張りなイヌ <福娘童話集 きょうのイソップ童話></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<table width="969" border="0" align="center" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td colspan="5" height="12">
<div align="center">
<table width="100%" border="0">
<tbody>
<tr>
<td width="27%" align="center"><a href="../../../index.html">福娘童話集</a> &gt; <a href="../index.html">きょうのイソップ童話</a></td>
<td width="46%" align="center"><a href="http://hukumusume.com/douwa/pc/aesop/index.html"><img src="../../../gazou/pc_gazou/all/aesop_logo_llll.gif" alt="福娘童話集 きょうのイソップ童話" width="320" height="100" border="0"></img></a></td>
<td width="27%" align="center" valign="bottom"><a href="http://hukumusume.com/douwa/index.html"><img src="../../../gazou/pc_gazou/all/douwa_logo_top_.gif" alt="童話・昔話・おとぎ話の福娘童話集" width="170" height="50" border="0"></img></a></td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
<tr>
<td width="166" height="830" valign="top">
<table width="166" border="0" cellpadding="0" cellspacing="0" bgcolor="#C8FFC8">
<tbody>
<tr>
<td height="7" valign="top"><img src="../../../../366/logo_bana/corner_1.gif" width="7" height="7"></img></td>
<td></td>
<td align="right" valign="top"><img src="../../../../366/logo_bana/corner_2.gif" width="7" height="7"></img></td>
</tr>
</tbody>
</table>
<table width="166" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td width="310" height="610" align="center" bgcolor="#C8FFC8">
<script type="text/javascript">
<!--
google_ad_client = "ca-pub-2746615155806331";
/* 1a月160x600 */
google_ad_slot = "0764542773";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
</tr>
</tbody>
</table>
</td>
<td width="619" valign="top">
<p align="center"><a href="../../../index.html">福娘童話集</a> &gt; <a href="../index.html">きょうのイソップ童話</a> &gt; <a href="../itiran/01gatu.htm">1月のイソップ童話</a> &gt; 欲張りなイヌ</p>
<p align="center">
<font color="#FF0000" size="+2">元旦のイソップ童話</font><br></br> <br></br> <img src="../../../gazou/pc_gazou/aesop/aesop052.jpg" alt="よくばりなイヌ" width="480" height="360" border="1"></img><br></br> <br></br> 欲張りなイヌ<br></br> <br></br> <a href="http://hukumusume.com/douwa/English/aesop/01/01_j.html">ひらがな</a> ←→ <a href="http://hukumusume.com/douwa/English/aesop/01/01_j&amp;E.html">日本語・英語</a> ←→ <a href="http://hukumusume.com/douwa/English/aesop/01/01_E.html">English</a></p>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td height="90" align="center">
<table width="80%" border="0" cellpadding="0" cellspacing="0" bgcolor="#C8FFC8">
<tbody>
<tr>
<td width="35%" height="25" valign="top"><img src="../../../../366/logo_bana/corner_1.gif" width="7" height="7"></img></td>
<td width="29%" align="center">
<font color="#FF0000"><b>おりがみをつくろう</b></font>
</td>
<td width="35%" valign="bottom">
<font size="-1">(<a href="http://www.origami-club.com/index.html"> おりがみくらぶ</a> より)</font>
</td>
<td width="1%" align="right" valign="top"><img src="../../../../366/logo_bana/corner_2.gif" width="7" height="7"></img></td>
</tr>
<tr>
<td height="80" colspan="4" align="center" valign="top">
<table width="98%" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td height="75" align="center" valign="middle" bgcolor="#ECFFEC">
<font size="+2"><a href="http://www.origami-club.com/easy/dogfase/index.html"><img src="../../../gazou/origami_gazou/kantan/dogface.gif" alt="犬の顔の折り紙" width="73" height="51" border="0"></img>いぬのかお</a></font>   <a href="http://www.origami-club.com/easy/dog/index.html"><img src="../../../gazou/origami_gazou/kantan/dog.gif" alt="犬の顔の紙" width="62" height="43" border="0"></img><font size="+2">いぬ</font></a></td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<table width="100%" border="0">
<tbody>
<tr>
<td align="center">♪音声配信(html5)</td>
</tr>
<tr>
<td align="center"><audio src="http://ohanashi2.up.seesaa.net/mp3/ae_0101.mp3" controls=""></audio></td>
</tr>
<tr>
<td align="center">
<font size="-1"><a href="http://www.voiceblog.jp/onokuboaki/">亜姫の朗読☆ イソップ童話より</a></font>
</td>
</tr>
</tbody>
</table>
<p>  肉をくわえたイヌが、橋を渡っていました。  ふと下を見ると、川の中にも肉をくわえたイヌがいます。 イヌはそれを見て、思いました。(あいつの肉の方が、大きそうだ)  イヌは、くやしくてたまりません。 (そうだ、あいつをおどかして、あの肉を取ってやろう)  そこでイヌは、川の中のイヌに向かって思いっきり吠えました。 「ウゥー、ワン!!」  そのとたん、くわえていた肉はポチャンと川の中に落ちてしまいました。 「ああー、ぁぁー」  川の中には、がっかりしたイヌの顔がうつっています。  さっきの川の中のイヌは、水にうつった自分の顔だったのです。  同じ物を持っていても、人が持っている物の方が良く見え、また、欲張るとけっきょく損をするというお話しです。</p>
<p align="center">おしまい</p>
<p align="center"><a href="javascript:history.back();" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Image10','','../../../gazou/pc_gazou/all/top_bana/back_logo_b.gif',1)"><img src="../../../gazou/pc_gazou/all/top_bana/back_logo_r.gif" alt="前のページへ戻る" name="Image10" width="175" height="32" border="0"></img></a> <br></br> <br></br>
<script type="text/javascript">
<!--
google_ad_client = "ca-pub-2746615155806331";
/* 1月336x280 */
google_ad_slot = "6046482409";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</p>
</td>
<td width="10"><img src="file:///C|/Documents and Settings/福娘note/デスクトップ/company_website15/image/spacer.gif" width="1" height="1"></img></td>
<td width="166" valign="top">
<table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#C8FFC8">
<tbody>
<tr>
<td height="7" valign="top"><img src="../../../../366/logo_bana/corner_1.gif" width="7" height="7"></img></td>
<td></td>
<td align="right" valign="top"><img src="../../../../366/logo_bana/corner_2.gif" width="7" height="7"></img></td>
</tr>
</tbody>
</table>
<table width="166" border="0" bgcolor="#C8FFC8">
<tbody>
<tr>
<td width="156" height="50">&nbsp;&nbsp;&nbsp;&nbsp;
<font size="-1"><b>1月 1日の豆知識</b></font><br></br>
<font size="-2"><u><br></br>
366日への旅</u></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><img src="file:///C|/Documents and Settings/福娘note/デスクトップ" width="1" height="1"></img><b><font size="-1">きょうの記念日</font></b><br></br>
<font size="-1"><a href="../../../../366/kinenbi/pc/01gatu/1_01.htm">元旦</a></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><img src="file:///C|/Documents and Settings/福娘note/デスクトップ/company_website15/image/spacer.gif" width="1" height="1"></img><b><font size="-1">きょうの誕生花</font></b><br></br>
<a href="../../../../366/hana/pc/01gatu/1_01.htm">
<font size="-1">松(まつ)</font>
</a>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">きょうの誕生日・出来事</font></b><br></br>
<font size="-1"><a href="../../../../366/birthday/pc/01gatu/1_01.htm">1949年 Mr.マリック(マジシャン)</a></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">恋の誕生日占い</font></b><br></br>
<font size="-1"><a href="../../../../sakura/uranai/birthday/01/01.html">自分の考えをしっかりと持った女の子。</a></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">なぞなぞ小学校</font></b><br></br>
<font size="-1"><a href="../../../../nazonazo/new/2012/04/02.html">○(丸)を取ったらお母さんになってしまう男の人は?</a></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">あこがれの職業紹介</font></b><br></br>
<font size="-1"><a href="../../../../sakura/navi/work/2017/041.html">歌手</a></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">恋の魔法とおまじない</font></b> 001 <br></br>
<font size="-1"><a href="../../../../omajinai/new/2012/00/re01.html">両思いになれる おまじない</a></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#C8FFC8">
<font size="-1">  <b>1月 1日の童話・昔話</b><br></br> <u><font size="-2"><br></br>
福娘童話集</font></u></font>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">きょうの日本昔話</font></b><br></br>
<a href="../../../../douwa/pc/jap/01/01.htm">
<font size="-1">ネコがネズミを追いかける訳</font>
</a>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">きょうの世界昔話<img src="file:///C|/Documents and Settings/福娘note/デスクトップ/company_website15/image/spacer.gif" width="1" height="1"></img></font></b><br></br>
<a href="../../../../douwa/pc/world/01/01a.htm">
<font size="-1">モンゴルの十二支話</font>
</a>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><img src="file:///C|/Documents and Settings/福娘note/デスクトップ/company_website15/image/spacer.gif" width="1" height="1"></img><b><font size="-1">きょうの日本民話</font></b><br></br>
<a href="../../../../douwa/pc/minwa/01/01c.html">
<font size="-1">仕事の取替えっこ</font>
</a>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">きょうのイソップ童話</font></b><br></br>
<a href="../../../../douwa/pc/aesop/01/01.htm">
<font size="-1">欲張りなイヌ</font>
</a>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">きょうの江戸小話</font></b><br></br>
<a href="../../../../douwa/pc/kobanashi/01/01.htm">
<font size="-1">ぞうきんとお年玉</font>
</a>
</td>
</tr>
<tr>
<td height="50" bgcolor="#ECFFEC"><b><font size="-1">きょうの百物語</font></b><br></br>
<a href="../../../../douwa/pc/kaidan/01/01.htm">
<font size="-1">百物語の幽霊</font>
</a>
</td>
</tr>
</tbody>
</table>
<table width="100%" border="0" bgcolor="#C8FFC8">
<tbody>
<tr>
<td height="30" align="center" bgcolor="#C8FFC8"><b><font size="-1">福娘のサイト</font></b></td>
</tr>
<tr>
<td height="60" bgcolor="#ECFFEC">
<font size="-1"><b>366日への旅</b><br></br> <a href="http://hukumusume.com/366/">毎日の記念日・誕生花 ・有名人の誕生日と性格判断</a></font>
</td>
</tr>
<tr>
<td height="60" bgcolor="#ECFFEC">
<font size="-1"><b>福娘童話集</b><br></br> <a href="http://hukumusume.com/douwa/">世界と日本の童話と昔話</a></font>
</td>
</tr>
<tr>
<td height="60" bgcolor="#ECFFEC">
<font size="-1"><b>女の子応援サイト -さくら-</b><br></br> <a href="http://hukumusume.com/sakura/index.html">誕生日占い、お仕事紹介、おまじない、など</a></font>
</td>
</tr>
<tr>
<td height="60" bgcolor="#ECFFEC">
<font size="-1"><b>子どもの病気相談所</b><br></br> <a href="http://hukumusume.com/my_baby/sick/">病気検索と対応方法、症状から検索するWEB問診</a></font>
</td>
</tr>
<tr>
<td height="60" bgcolor="#ECFFEC">
<font size="-1"><b>世界60秒巡り</b><br></br> <a href="http://hukumusume.com/366/world/">国旗国歌や世界遺産など、世界の国々の豆知識</a></font>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</body>
</html>
Loading…
Cancel
Save