Skip to content

Commit

Permalink
Conforms to new microformats tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glennjones committed Jun 14, 2015
1 parent 6d624e7 commit c44e1da
Show file tree
Hide file tree
Showing 219 changed files with 2,723 additions and 18,328 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ results

node_modules
npm-debug.log
temp
temp-tests
testx
_site
.DS_Store

Expand Down
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function( grunt ) {
}
},
mochaTest: {
files: ['test/*-test.js']
files: ['test/*-test.js','mf-test/*-test.js']
},
watch: {
files: 'lib/*.js',
Expand Down
121 changes: 29 additions & 92 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,24 @@ h-adr, h-card, h-entry, h-event, h-geo, h-news, h-product, h-recipe, h-resume, h


### Install

```bash
npm install microformat-node

or

git clone http://github.com/glennjones/microformat-node.git
cd microformat-node
npm link

```

### Use

#### using a callback

```javascript
var microformats = require("microformat-node"),
options = {};

microformats.parseUrl('http://glennjones.net/about', options, function(err, data){
// do something with data
});

```

#### using a promise

```javascript
var microformats = require("microformat-node"),
options = {};

Expand All @@ -47,69 +41,62 @@ or
// do something with error
}
)

```
### Main parse function

#### parseUrl()

```javascript
var microformats = require("microformat-node"),
options = {};

microformats.parseUrl('http://glennjones.net/about', options, function(err, data){
// do something with data
});

```
#### parseHtml()

```javascript
var microformats = require("microformat-node"),
options = {};

var html = '<p class="vcard"><a class="fn url" href="http://glennjones.net">Glenn Jones</a></p>';
microformats.parseHtml(html, options, function(err, data){
// do something with data
});

```

#### parseDom()
This function takes both a [Cheerio](https://github.com/MatthewMueller/cheerio) DOM and node object.

```javascript
var microformats = require("microformat-node"),
options = {};

microformats.parseDom(dom, node, options function(err, data){
// do something with data
});

```

### Parsing options

#### Example use of options
```javascript
var microformats = require("microformat-node"),
options = {'filters': ['h-card']};
options = {'dateFormat': 'HTML5'};

microformats.parseUrl('http://glennjones.net/about', options, function(err, data){
// do something with data
});
```

#### Available options
* `baseUrl` - this is used to revolve any relative URLs in the output
* `textFormat` - (string) plain text output style: `normalised`, `whitespace` or `whitespacetrimmed`. The default is `whitespacetrimmed`
* `dateFormat` - (string) ISO date profiles used in output text output style `auto`, `W3C`, `RFC3339`, `HTML`. The profile `auto` outputs the date as authored. The default is `auto`

* filters - (array) an array of formats to filter the output by i.e. ['h-card','h-geo']. - default is empty which displays all formats.
* baseUrl - this is used to revolve any relative URLs in the output
* version1 - (boolean) whether the output should contain version 1 microformats. - default is true
* rel - (boolean) whether the output should contain rel=*. - default is true
* children - (boolean) whether the output should contain children. - default is true
* textFormat - (string) plain text output style 'normalised' or 'whitespace' or 'whitespacetrimmed' default is 'normalised'
* logLevel - (int 0-4) set the level at which the parser logs events - default is 4
* useCache - (boolean) whether a parse should use the HTML cache. - default is false
* cacheTimeLimit - (int) the amount of time items are kept in the cache before they are discarded. The time is set in milliseconds. - default is 360000
* cacheItemLimit - (int) the number of items to keep in cache before some are discarded - default is 1000
* cache - (object) an object containing an interface described in the Custom cache section of this document.
* logger - (object) an object containing an interface described in the Custom logger section of this document.


### Response

__Version 0.2.0 was a complete rewrite of microformat-node to conform to the new version 2 specification of microformats. If you used the older 0.1.x versions of microformat-node you will find the JSON output has changed. The output changes were designed to bring it closer to the microdata specification.__
__Version 0.2.x upwards a was a complete rewrite of microformat-node to conform to the new version 2 specification of microformats. If you used the older 0.1.x versions of microformat-node you will find the JSON output has changed Version 0.3.x has addtional rel-url collection and some modification to the normalisation of text whitespace and ISO date structures .__

Typical data structure. This is an example of a h-card microformat.

Expand All @@ -124,6 +111,8 @@ Typical data structure. This is an example of a h-card microformat.
"category": ["Strategy", "Leadership"]
}
}]
"rels": {},
"rel-urls": {}
}

Typical error structure.
Expand All @@ -133,57 +122,6 @@ Typical error structure.
"error": "Error: Invalid protocol - xhttp://microformats.org/"
}]
}


## Custom cache

microformats-node uses an in-memory cache to store the HTML of web pages.

The options object contains a property called `cacheTimeLimit` that can be used to set the cache refresh time. By default, this is 3600000ms. The number of items stored in the cache can be limited using the options property `cacheItemLimit`. By default, the cache is limited to 1000 items. The 'useCache' property of the options object is set to false by default.

You can replace the cache with your own, for example, to store the cached data in a database or file system. To add you own custom cache, all you need to do is provide an object containing the following interface:

{
function get (url) {
// add code to get data
returns data
}

function has(url) {
// add code to check your data store
returns true or false
}

function fetch (url, callback) {
// add code to return data
fires callback(null, data);
}

function set(url, data) {
// add code to store data
returns object
}

function setCacheLimits (newCacheTimeLimit, newCacheItemLimit) {
// can be used to collect setting from options
}
}

and then add this interface as the `cache` property of the options object passed into the `parseUrl()` or `parseHtml()` methods.


### Custom logger

microformats-node uses a simple logging system that writes to Node's console. You can replace the logger with your own, for example, to store warnings and errors in a database or log file. To add your own custom logger, all you need to do is provide an object containing the following interface:

{
function info (message) { /* code to pass on message */ }
function log (message) { /* code to pass on message */ }
function warn (message) { /* code to pass on message */ }
function error(message) { /* code to pass on message */ }
}

and then add this interface to the `logger` property of the options object passed into the `parseUrl()`, `parseHtml()` or `parseDom()` methods.


### Querying demo server
Expand All @@ -202,15 +140,14 @@ Then visit the server URL
You need to provide the url of the web page:

GET http://localhost:8888/?url=http%3A%2F%2Flocalhost%3A8888%2Ftest%2F

### Viewing the tests

The module includes web pages which runs the [microfomats 2 test suite](https://github.com/microformats/tests). Once you have started the server using the details above you can view the [mocha](http://visionmedia.github.com/mocha/) tests in a browser. If you install mocha you can also run the tests from the command line.

http://localhost:8888/test/mocha-v1.html
http://localhost:8888/test/mocha-v2.html




### Mocha test
The project has a number of test taken from [microfomats 2 test suite](https://github.com/microformats/tests). To run the test within the project type the following command.
```bash
$ mocha --reporter list
```
If you are considering sending a pull request please add tests for the functionality you add or change.

### Support or Contact

Expand Down
2 changes: 1 addition & 1 deletion lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function Parser() {
'children': true,
'rel': true,
'includes': true,
'textFormat': 'impliednametrimmed',
'textFormat': 'whitespacetrimmed',
'dateFormat': 'auto',
'protocals': ['http','https','ftp','ftps','mailto','tel','sms'],
'cacheTimeLimit': 3600000,
Expand Down
2 changes: 1 addition & 1 deletion lib/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ exports.textContent = textContent;


function Text(){
this.textFormat = 'impliednametrimmed'; // normalised or whitespace or whitespacetrimmed or impliednametrimmed - used as default
this.textFormat = 'whitespacetrimmed'; // normalised or whitespace or whitespacetrimmed or impliednametrimmed - used as default
this.blockLevelTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p', 'hr', 'pre', 'table',
'address', 'article', 'aside', 'blockquote', 'caption', 'col', 'colgroup', 'dd', 'div',
'dt', 'dir', 'fieldset', 'figcaption', 'figure', 'footer', 'form', 'header', 'hgroup', 'hr',
Expand Down
2 changes: 1 addition & 1 deletion license.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License - microformat-node

Copyright (c) 2012-2014 Glenn Jones
Copyright (c) 2012-2015 Glenn Jones

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@
"microformat-node": "./bin/microformat-node"
},
"dependencies": {
"request": "2.55.x",
"cheerio": "0.19.x",
"download-github-repo": "^0.1.3",
"entities": "1.1.x",
"fs-extra": "^0.19.0",
"github-download": "^0.3.0",
"request": "2.55.x",
"when": "3.7.x"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit c44e1da

Please sign in to comment.