Skip to content

Commit

Permalink
Update example to 4.1.0, Add new XP-support
Browse files Browse the repository at this point in the history
  • Loading branch information
FokkeZB committed Aug 8, 2015
1 parent 7a93253 commit 3d26f47
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 41 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Dependencies](https://david-dm.org/fokkezb/ti-html2as/status.svg?style=flat-square)](https://david-dm.org/fokkezb/ti-html2as#info=dependencies)
[![Dev Dependencies](https://david-dm.org/fokkezb/ti-html2as/dev-status.svg?style=flat-square)](https://david-dm.org/fokkezb/ti-html2as#info=devDependencies)

HTML to [Ti.UI.iOS.AttributedString](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.iOS.AttributedString) parser for [Titanium](http://appcelerator.com/titanium).
HTML to [Ti.UI.AttributedString](http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.AttributedString) parser for [Titanium](http://appcelerator.com/titanium).

## Screencast

Expand All @@ -14,7 +14,7 @@ A packaged *CommonJS* module can be found via [Releases](https://github.com/fokk

gittio install nl.fokkezb.html2as

The module exports a single function that takes an HTML string and a callback to receive an error or [Ti.UI.iOS.AttributedString](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.iOS.AttributedString) object.
The module exports a single function that takes an HTML string and a callback to receive an error or [Ti.UI.AttributedString](http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.AttributedString) object.

```
var html2as = require('nl.fokkezb.html2as');
Expand Down Expand Up @@ -91,6 +91,7 @@ grunt

## Changelog

* 1.3.0: Adds support for `Ti.UI.AttributedString`, backwards compatible with `Ti.UI.iOS.AttributedString`.
* 1.2.0: Adds [support for HTML entities](https://github.com/FokkeZB/ti-html2as/pull/5)
* 1.1.0: Updated for Titaniumifier 1.0.0
* 1.0.0: Initial version
Expand Down
4 changes: 2 additions & 2 deletions example/app/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ html2as(
'<font size="30">font:size</font>\n' +
'<font color="red">font:color</font>\n' +
'<font face="AmericanTypewriter" size="8" color="red">font:face+size+color</font>\n' +
'<a href="test.html">a:href</a> (longtap)\n' +
'<a href="test.html">a:href</a> (longtap with older SDKs)\n' +
'and character entities: <strong>&amp;</strong> <em>&copy;</em> \n' +
'Hall&ograve; <b>world</b> \n' +
'\nNon-standard attributes:\n\n' +
Expand Down Expand Up @@ -46,5 +46,5 @@ html2as(
});

function onLink(e) {
alert('Longtap on link to: ' + e.url);
alert('Handle link to: ' + e.url);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
var htmlparser = require("htmlparser2");
var entities = require("entities");

var ns = Ti.UI.createAttributedString ? Ti.UI : Ti.UI.iOS;

function walker(node, parameters, outerFont) {

if (node.type === 'text') {
Expand Down Expand Up @@ -52,49 +54,56 @@ function walker(node, parameters, outerFont) {

if (node.name === 'a' && node.attribs && node.attribs.href) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_LINK,
type: ns.ATTRIBUTE_LINK,
value: node.attribs.href,
range: [offset, length]
});

} else if (node.name === 'u') {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_UNDERLINES_STYLE,
value: Ti.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
type: ns.ATTRIBUTE_UNDERLINES_STYLE,
value: ns.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
range: [offset, length]
});

} else if (node.name === 'i' || node.name === 'em') {
parameters.attributes.unshift({
type: ns.ATTRIBUTE_OBLIQUENESS,
value: 0.25,
range: [offset, length]
});

} else if (node.name === 'strike' || node.name === 'del' || node.name === 's') {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_STRIKETHROUGH_STYLE,
value: Ti.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
type: ns.ATTRIBUTE_STRIKETHROUGH_STYLE,
value: ns.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
range: [offset, length]
});

} else if (node.name === 'effect') {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_TEXT_EFFECT,
value: Ti.UI.iOS.ATTRIBUTE_LETTERPRESS_STYLE,
type: ns.ATTRIBUTE_TEXT_EFFECT,
value: ns.ATTRIBUTE_LETTERPRESS_STYLE,
range: [offset, length]
});

} else if (node.name === 'kern' && node.attribs && node.attribs.value) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_KERN,
type: ns.ATTRIBUTE_KERN,
value: node.attribs.value,
range: [offset, length]
});

} else if (node.name === 'expansion' && node.attribs && node.attribs.value) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_EXPANSION,
type: ns.ATTRIBUTE_EXPANSION,
value: node.attribs.value,
range: [offset, length]
});

} else if (node.name === 'font' && node.attribs && node.attribs.color) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_FOREGROUND_COLOR,
type: ns.ATTRIBUTE_FOREGROUND_COLOR,
value: node.attribs.color,
range: [offset, length]
});
Expand All @@ -103,7 +112,7 @@ function walker(node, parameters, outerFont) {
// if we have a font to set
if (innerFont) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_FONT,
type: ns.ATTRIBUTE_FONT,
value: innerFont,
range: [offset, length]
});
Expand All @@ -123,8 +132,6 @@ module.exports = function(html, callback) {

} else {

// console.debug(dom);

var parameters = walker({
type: 'tag',
children: dom
Expand All @@ -133,17 +140,19 @@ module.exports = function(html, callback) {
attributes: []
});

// console.log(parameters);

var attr = Titanium.UI.iOS.createAttributedString(parameters);
var attr = ns.createAttributedString(parameters);

callback(null, attr);
}
}));

// remove newlines
html = html.replace(/[\r\n]+/gm, ' ').replace(/\s+/g, ' ');

parser.parseComplete(html);

};

},{"entities":2,"htmlparser2":46}],2:[function(require,module,exports){
var encode = require("./lib/encode.js"),
decode = require("./lib/decode.js");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"devDependencies": {
"grunt-contrib-clean": "^0.6.0",
"grunt-titanium": "^0.3.1",
"grunt-titanium": "fokkezb/grunt-titanium#patch-3",
"grunt-titaniumifier": "^1.0.0-beta",
"grunt-zip": "^0.16.0",
"grunt": "^0.4.2"
Expand Down
3 changes: 2 additions & 1 deletion example/plugins/ti.alloy/hooks/alloy.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ exports.init = function (logger, config, cli, appc) {
// we have no clue where alloy is installed, so we're going to subprocess
// alloy and hope it's in the system path or a well known place
var paths = {};
var locatorCmd = process.platform === 'win32' ? 'where' : 'which';
parallel(this, ['alloy', 'node'].map(function (bin) {
return function (done) {
var envName = 'ALLOY_' + (bin === 'node' ? 'NODE_' : '') + 'PATH';
Expand All @@ -94,7 +95,7 @@ exports.init = function (logger, config, cli, appc) {
paths.alloy = 'alloy.cmd';
done();
} else {
exec('which ' + bin, function (err, stdout, strerr) {
exec(locatorCmd + ' ' + bin, function (err, stdout, strerr) {
if (!err) {
paths[bin] = stdout.trim();
done();
Expand Down
2 changes: 1 addition & 1 deletion example/tiapp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<target device="iphone">true</target>
<target device="mobileweb">false</target>
</deployment-targets>
<sdk-version>3.5.1.GA</sdk-version>
<sdk-version>4.1.0.GA</sdk-version>
<plugins><plugin version="1.0">ti.alloy</plugin>
</plugins>
</ti:app>
32 changes: 15 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var htmlparser = require("htmlparser2");
var entities = require("entities");

var ns = Ti.UI.createAttributedString ? Ti.UI : Ti.UI.iOS;

function walker(node, parameters, outerFont) {

if (node.type === 'text') {
Expand Down Expand Up @@ -51,56 +53,56 @@ function walker(node, parameters, outerFont) {

if (node.name === 'a' && node.attribs && node.attribs.href) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_LINK,
type: ns.ATTRIBUTE_LINK,
value: node.attribs.href,
range: [offset, length]
});

} else if (node.name === 'u') {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_UNDERLINES_STYLE,
value: Ti.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
type: ns.ATTRIBUTE_UNDERLINES_STYLE,
value: ns.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
range: [offset, length]
});

} else if (node.name === 'i' || node.name === 'em') {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_OBLIQUENESS,
type: ns.ATTRIBUTE_OBLIQUENESS,
value: 0.25,
range: [offset, length]
});

} else if (node.name === 'strike' || node.name === 'del' || node.name === 's') {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_STRIKETHROUGH_STYLE,
value: Ti.UI.iOS.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
type: ns.ATTRIBUTE_STRIKETHROUGH_STYLE,
value: ns.ATTRIBUTE_UNDERLINE_STYLE_SINGLE,
range: [offset, length]
});

} else if (node.name === 'effect') {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_TEXT_EFFECT,
value: Ti.UI.iOS.ATTRIBUTE_LETTERPRESS_STYLE,
type: ns.ATTRIBUTE_TEXT_EFFECT,
value: ns.ATTRIBUTE_LETTERPRESS_STYLE,
range: [offset, length]
});

} else if (node.name === 'kern' && node.attribs && node.attribs.value) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_KERN,
type: ns.ATTRIBUTE_KERN,
value: node.attribs.value,
range: [offset, length]
});

} else if (node.name === 'expansion' && node.attribs && node.attribs.value) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_EXPANSION,
type: ns.ATTRIBUTE_EXPANSION,
value: node.attribs.value,
range: [offset, length]
});

} else if (node.name === 'font' && node.attribs && node.attribs.color) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_FOREGROUND_COLOR,
type: ns.ATTRIBUTE_FOREGROUND_COLOR,
value: node.attribs.color,
range: [offset, length]
});
Expand All @@ -109,7 +111,7 @@ function walker(node, parameters, outerFont) {
// if we have a font to set
if (innerFont) {
parameters.attributes.unshift({
type: Ti.UI.iOS.ATTRIBUTE_FONT,
type: ns.ATTRIBUTE_FONT,
value: innerFont,
range: [offset, length]
});
Expand All @@ -129,8 +131,6 @@ module.exports = function(html, callback) {

} else {

// console.debug(dom);

var parameters = walker({
type: 'tag',
children: dom
Expand All @@ -139,9 +139,7 @@ module.exports = function(html, callback) {
attributes: []
});

// console.log(parameters);

var attr = Titanium.UI.iOS.createAttributedString(parameters);
var attr = ns.createAttributedString(parameters);

callback(null, attr);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"devDependencies": {
"grunt-contrib-clean": "^0.6.0",
"grunt-titanium": "^0.3.1",
"grunt-titanium": "fokkezb/grunt-titanium#patch-3",
"grunt-titaniumifier": "^1.0.0-beta",
"grunt-zip": "^0.16.0",
"grunt": "^0.4.2"
Expand Down

0 comments on commit 3d26f47

Please sign in to comment.