Skip to content

Instantly share code, notes, and snippets.

@youxiachai
Last active December 17, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save youxiachai/5521332 to your computer and use it in GitHub Desktop.
Save youxiachai/5521332 to your computer and use it in GitHub Desktop.
var jsdom = require("jsdom");
jsdom.env({
html: "http://www.oschina.net/translate/list?type=2",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (errors, window) {
var $ = window.$;
//获取页数
// var lastPage = $('ul.pager li').slice(-2, -1).text();
// console.log("lastPage-->" + lastPage);
//初始化内容列表
var listContent = "";
$('div.article').each(function (index, item) {
//封装jquery 对象
var $body = $(item);
//解析原始链接,过去path路径
var itemPath = urlUtils.parse($body.find('dt a').attr('href')).pathname;
//console.log("index->" + index + "->" + itemPath);
//build Title :li header h2 a
//太傻了,可以用replace with 优雅的完成...以后再改吧,佩服自己的傻劲..
var header = $body.find('dt a').attr("href", itemPath).removeAttr("target").unwrap().wrap('<header>').wrap('<h5/>').parent();
// console.log("index->" + index + "->"+header.html());
//build DateTime : div a
var dateTime = $body.find('dd.remark').wrap("<div />").find('a').unwrap().parent();
// console.log("index->" + index + "->"+dateTime.html());
//insert dateTime after title
header.after(dateTime);
//replace dd.content --> div
// var $item = $item.replaceWith("<div>"+itemText + "</div>");
$body.find('dd.content').replaceWith(function () {
var itemText = $(this).text();
return "<div>" + itemText + "</div>";
});
//build review: replace dl -> div
$body.find('dl').wrap("<li />").replaceWith(function () {
var html = $(this).wrapInner("<div />").children().addClass("content");
return html;
});
//console.log("index->" + index + "->" + $body.html());
listContent += $body.wrap("<li />").html();
});
var listhtml = "<ul class='nav nav-list'>" + listContent + "</ul>";
window.close();
});
var listhtml = "<ul class='nav nav-list'>" + listContent + "</ul>";
window.close();
console.log(listhtml);
}
});
var jsdom = require("jsdom");
jsdom.env({
html: "http://www.oschina.net/translate/list?type=2",
scripts: ["http://code.jquery.com/jquery.js"],
done: function (errors, window) {
var $ = window.$;
//获取页数
// var lastPage = $('ul.pager li').slice(-2, -1).text();
// console.log("lastPage-->" + lastPage);
//初始化内容列表
var listContent = "";
$('div.article').each(function (index, item) {
//封装jquery 对象
var $body = $(item);
//解析原始链接,过去path路径
var itemPath = urlUtils.parse($body.find('dt a').attr('href')).pathname;
//console.log("index->" + index + "->" + itemPath);
//build Title :li header h2 a
//太傻了,可以用replace with 优雅的完成...以后再改吧,佩服自己的傻劲..
var header = $body.find('dt a').attr("href", itemPath).removeAttr("target").unwrap().wrap('<header>').wrap('<h5/>').parent();
// console.log("index->" + index + "->"+header.html());
//build DateTime : div a
var dateTime = $body.find('dd.remark').wrap("<div />").find('a').unwrap().parent();
// console.log("index->" + index + "->"+dateTime.html());
//insert dateTime after title
header.after(dateTime);
//replace dd.content --> div
// var $item = $item.replaceWith("<div>"+itemText + "</div>");
$body.find('dd.content').replaceWith(function () {
var itemText = $(this).text();
return "<div>" + itemText + "</div>";
});
//build review: replace dl -> div
$body.find('dl').wrap("<li />").replaceWith(function () {
var html = $(this).wrapInner("<div />").children().addClass("content");
return html;
});
//console.log("index->" + index + "->" + $body.html());
listContent += $body.wrap("<li />").html();
});
var listhtml = "<ul class='nav nav-list'>" + listContent + "</ul>";
window.close();
console.log(listhtml);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment