手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表2011年08月的文章

文章阅读时启用readability插件

OK,看我博客的人,可能会看到这个工具条了,嗯,我已经在网站的阅读时候启用了readability这个插件,如果不想看到文章左右的广告的用户,可以点击NOW看一下。当然如果你对文章有兴趣,你可以点击EMAIL发到邮箱。

更重要的是,如果你有kindle,你也可以尝试发送过去。(希望我转载的文章足够吸引人)

放心吧,不要以为很难,其实 你也可以拥有这样的功能。在你的页面上加上这样一段代码就OK了:

<div class="rdbWrapper" data-show-read="1" data-show-send-to-kindle="1" data-show-print="1" data-show-email="1" data-version="1"></div><script type="text/javascript"> (function() { var s = document.getElementsByTagName("script")[0], rdb = document.createElement("script"); rdb.type = "text/javascript"; rdb.async = true; rdb.src = document.location.protocol + "//www.readability.com/embed.js"; s.parentNode.insertBefore(rdb, s); })(); </script>

什么,复制代码无效?没关系,看这里:http://www.readability.com/publishers/tools

你可以自定义你要的样式啦

Tags: readability

HTML Purifier库

好吧,上文我说到了HtmlPurifier这个类库,很多人还是云里雾里的,确实,对于国外那些相对比较标准的页面来说,嗯。这些都确实是云里雾里的,但国内还是需要这样的。
它适用于几种场景
1、采集,这个就不用说啦,采的不标准的话,HTML 标签可能不闭合
2、所见所得编辑器:如果COPY一段网上的文章进所见所得编辑器,可能会不闭合,因为你不能保证你复制的内容是一定闭合(大部分编辑器会帮你修复这种问题)
3、还是所见所得编辑器。在这种编辑器里,如果你在内容里加上分页标记,再由他自动分页,则可能会遇到HTML标记不全的情况(这在supesite中特别 多见,但可以理解)

因此,有PHP tidy库的人就有福了,但tidy库需要在服务器上编译,不是很方便,毕竟不是每个人都有服务器的。所以,HtmlPurifier类库就起作用了。
官方这么介绍:

XML/HTML代码
  1. HTML Purifier is a standards-compliant  HTML filter library written in   PHP. HTML Purifier will not only remove all malicious   code (better known as XSS) with a thoroughly audited,    secure yet permissive whitelist,    it will also make sure your documents are     standards compliant, something only achievable with a     comprehensive knowledge of W3C's specifications.     Tired of using BBCode due to the current landscape of deficient or     insecure HTML filters? Have a  WYSIWYG editor but never been able to use it? Looking     for high-quality, standardscompliant, open-source components for that     application you're building? HTML Purifier is for you!  

如果对这个类库还不清楚,你可以看一下这里的demo:http://htmlpurifier.org/demo.php
尝试输入<div><p></div>
然后提交一下,你就知道了

Tags: htmlpurifier

基于PHP版本的 Readability 库

说起readability或许知道的人还真的不是特别多,其实 这是一个工作室,叫arc90的开发的一个JS类库,作用是方便各种掌上设备在阅读页面的时候可以只关注真正的内容部分,而忽略掉那些无关的广告或者其他标记。
这,对于国内的开发人员来说,那就是采集利器了,因为在这种情况下,页面里剩下的内容就只有文章主体了。这个结果,你懂的。
arc90这个工作室或许很多人不知道,但flipboard很多人应该听说过,做IPAD上资讯类阅读软件开发的人员应该会知道这个软件啦。
不多说了,我这里是转的PHP版的readability的库,上面的一些内容,转载的文章中也有介绍,可以LOOK一下先,原文来自:http://blog.zol.com.cn/2615/article_2614448.html

阅读是汲取知识的途径之一,但烦杂的网页提了额外我们诸多不需要的信息,这有时候非常干扰我们阅读。回归阅读 内容的本身,其实通过Readability 这个脚本 就能抽取页面的主体内容。

不过,Readability 提供的仅仅是个 Bookmarklet,如果我们需要将其导出、打印等操作时就显 得捉衿见肘。需求驱动行动,于是就有了这个 PHP 库。PS,同时您还可以找到 .net 版本的 Readability,以及 node.js 版本的 Readability

这个库能做的事情非常的简单,就是找到页面的主体内容并返回。调用这个库非常的容易,实例代码如下:

PHP代码
  1. require 'lib/Readability.inc.php';  
  2. $Readability = new Readability($html$html_input_charset); // default charset is utf-8  
  3. $ReadabilityData = $Readability->getContent();    
  4. echo "".$ReadabilityData['title']."";   
  5. echo $ReadabilityData['content'];  

如果您不在意技术本身,那么这里有个现成的线上工具。只需要粘贴入需要阅读的网页链接,这个工具就能够重新生成适合阅读的页面(例如: 原页面Readability 以后的页面)。

大小: 185.14 K
尺寸: 495 x 376
浏览: 1369 次
点击打开新窗口浏览全图

然后利用 Mac 的 pdf 打印输出功能,就能非常方便在 kindle 等阅读器上使用。

Q&A

输出的页面似乎有乱码?

PHP Readability 库能够指定输入的字符编码并统一返回 utf-8 编码的字符串。如果您在处理过程中碰到了乱码问题,请调整下字符编码。如果还是有问题,欢迎您的反馈。

有些页面提取内容失败或者提取不正确?

Readability 提取的算法其实非常有限。如果页面本身的 HTML 标签不是非常合乎规范,那么提取可能会造成一定的困扰。通常这在国内的页面上会更常见些,同样欢迎您的反馈,这有利于我继续改进 PHP Readability 的算法。

最后,希望这个库和工具能让你找回阅读的乐趣。

------------------------
嗯,在使用这个软件的时候,如果发现HTML标准不规范(你懂的,国内的网页,有超过50%以上的页面是不规范的,嗯,标签不闭合的都有很多),请使用PHP的tidy库修复一下,如果没有tidy库,那就用htmlpurifier修复一下吧。
顺便:YII的自带扩展中已经包含htmlpurifier插件。

 

Tags: readability, arc90, flipboard, tidy, htmlpurifier

一个疏忽引发的血案

这是一个PHP的函数,可以把相对路径转为绝对路径。比如可以把http://www.neatstudio.com/archive/../upload/xxx.jpg,转换成http://www.neatstudio.com/upload/xxx.jpg

PHP代码
  1. function getRealUrl($url) {  
  2.         $path = parse_url($url, PHP_URL_PATH);  
  3.         $patharr = explode("/"$path);  
  4.         $newarr = array();  
  5.         $i = 0;  
  6.         foreach ($patharr as $k => $val) {  
  7.             if ($k != 0 && !$val) {  
  8.                 continue;  
  9.             }  
  10.             if ($val == '..' && count($newarr) > 1) {  
  11.                 unset($newarr[$i - 1]);  
  12.                 $i--;  
  13.                 continue;  
  14.             }  
  15.             $newarr[$i] = $val;  
  16.             $i++;  
  17.         }  
  18.         $newpath = join("/"$newarr);  
  19.         return str_replace($path$newpath$url);  
  20.     }  

正常情况下,都没什么问题。但某一天,居然遇到了这样的网址:http://neatstudio.com/xxx/xxx/../0/abc.jpg,于是就出错了,返回成http://neatstudio.com/xxx/abc.jpg
细心的朋友可能发现了上面的问题,/0这个目录不见了。这是为什么呢??

居然是因为第7行 ,我对$val的判断是!$val。。。它认为0目录是不为真就跳过了。TNND,所以我改成了$val == '',真纠结。

粗心啊(请不要纠结上面的代码。花10分钟写的东西,质量不高是肯定的。)

几个无限分类的设计

之前在博客里贴过无限分类的介绍,那是官方的例子,但上次在给同事们做介绍的时候,却发现官方的网站已经打不开了。所幸,又找到了一篇,好象这个才是原文?
原文来自:http://www.vbmysql.com/articles/database-design/managing-hierarchical-data-in-mysql
为了这次保证所有的数据都能够记录下来而不是再被茫茫的互联网淹没,所以我做了备份。
mysql.7z
但同样的,我也再次复制一部分的内容下来,好象是说这种无限分类的效率,在被其他项目查询的时候是最高的,但CRUD的时候就有点纠结。
OK,上主菜:

The Nested Set Model

What I would like to focus on in this article is a different approach, commonly referred to as the Nested Set Model. In the Nested Set Model, we can look at our hierarchy in a new way, not as nodes and lines, but as nested containers. Try picturing our electronics categories this way:

Nested Categories

Notice how our hierarchy is still maintained, as parent categories envelop their children.We represent this form of hierarchy in a table through the use of left and right values to represent the nesting of our nodes:

CREATE TABLE nested_category (         category_id INT AUTO_INCREMENT PRIMARY KEY,         name VARCHAR(20) NOT NULL,         lft INT NOT NULL,         rgt INT NOT NULL );  INSERT INTO nested_category VALUES(1,'ELECTRONICS',1,20),(2,'TELEVISIONS',2,9),(3,'TUBE',3,4),  (4,'LCD',5,6),(5,'PLASMA',7,8),(6,'PORTABLE ELECTRONICS',10,19),(7,'MP3 PLAYERS',11,14),(8,'FLASH',12,13),  (9,'CD PLAYERS',15,16),(10,'2 WAY RADIOS',17,18);  SELECT * FROM nested_category ORDER BY category_id;  +-------------+----------------------+-----+-----+ | category_id | name                 | lft | rgt | +-------------+----------------------+-----+-----+ |           1 | ELECTRONICS          |   1 |  20 | |           2 | TELEVISIONS          |   2 |   9 | |           3 | TUBE                 |   3 |   4 | |           4 | LCD                  |   5 |   6 | |           5 | PLASMA               |   7 |   8 | |           6 | PORTABLE ELECTRONICS |  10 |  19 | |           7 | MP3 PLAYERS          |  11 |  14 | |           8 | FLASH                |  12 |  13 | |           9 | CD PLAYERS           |  15 |  16 | |          10 | 2 WAY RADIOS         |  17 |  18 | +-------------+----------------------+-----+-----+

We use lft and rgt because left and right are reserved words in MySQL, see http://dev.mysql.com/doc/mysql/en/reserved-words.html for the full list of reserved words.

So how do we determine left and right values? We start numbering at the leftmost side of the outer node and continue to the right:

Numbering edges

This design can be applied to a typical tree as well:

Numbered Tree

When working with a tree, we work from left to right, one layer at a time, descending to each node’s children before assigning a right-hand number and moving on to the right. This approach is called the modified preorder tree traversal algorithm.

因为我已经在压缩包里做了备份,所以我图片就引用外站的了。

Tags: mysql, 无限分类

Records:3812345678