您的位置:首页 > 其它

flex_(5)正则表达式_WikiEditor解析程序;

2012-06-14 14:56 387 查看
=>WikiEditor.mxml

<?xml version="1.0" encoding="utf-8"?>

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

xmlns:s="library://ns.adobe.com/flex/spark"

xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"

creationComplete="creationCompleteHandler(event)">

<fx:Script>

<![CDATA[

import com.cen.programmingas3.wikiEditor.CurrencyConverter;

import com.cen.programmingas3.wikiEditor.URLParser;

import com.cen.programmingas3.wikiEditor.WikiParser;

import mx.events.FlexEvent;

/*转换类*/

private var wikiParser:WikiParser;

protected function creationCompleteHandler(event:FlexEvent):void

{

/*初始化_wikiParser示例已经包含原始数据*/

wikiParser = new WikiParser();

inputText.text = wikiParser.wikiData;

outputText.text = wikiParser.parseWikiString(inputText.text);

}

protected function testButton_clickHandler(event:MouseEvent):void

{

var outStr:String;

outStr = wikiParser.parseWikiString(inputText.text);

if(dollarToEuro.selected) {

outStr = CurrencyConverter.usdToEuro(outStr);

}

if(urlToATag.selected) {

outStr = URLParser.urlToATag(outStr);

}

outputText.text = outStr;

}

]]>

</fx:Script>

<!--wiki解析程序-->

<s:VGroup width="650" height="450" verticalAlign="middle" verticalCenter="0" horizontalAlign="center"

horizontalCenter="0">

<s:Panel width="100%" height="100%" title="解析程序_原始字符串">

<s:VGroup width="100%" height="100%">

<s:TextArea id="inputText" width="100%" height="100%"/>

<s:HGroup width="100%" horizontalAlign="right">

<s:CheckBox id="dollarToEuro" label="$ to €"/>

<s:CheckBox id="urlToATag" label="URLs to <a>"/>

<s:Button id="testButton" label="转换" click="testButton_clickHandler(event)"/>

</s:HGroup>

</s:VGroup>

</s:Panel>

<s:Panel width="100%" height="100%" title="HTML文本">

<s:TextArea id="outputText" width="100%" height="100%"/>

</s:Panel>

</s:VGroup>

</s:Application>

=>WikiParser .as

package com.cen.programmingas3.wikiEditor

{

/**

* wiki解析类

* - 使用正则表达式将wiki字符串转换成HTML文本;

*/

public class WikiParser {

/**

* 属性*/

/**

* wiki字符串

*/

public var wikiData:String;

/**

* 构造函数:使用原始数据初始化wikiData属性;

*/

public function WikiParser() {

wikiData = setWikiData();

}

/**

* 返回原始wiki数据

*/

private function setWikiData():String {

/*原始数据*/

var str:String = "'''Test wiki data'''\n" +
// 加粗字体;

"\n" +

"This is a test. This is ''only'' a test.\n" +
// 斜体;

"Basic rules:\n" +

"* 3 single quote marks indicates '''bold'''.\n" +

"* 2 single quote marks indicates ''italics''.\n" +

"* An asterisk creates a bulleted list item.\n" +

"* Use blank lines as paragraph separators.\n" +

"\n" +

"You can convert a dollar value like this: $9.95.\n" +

"\n" +

"Here's a URL to convert: http://www.adobe.com.\n" +

"\n" +

"Here's an e-mail address to convert: mailto:bob@example.com.";

return str;

}

/**

* 转换方法:

* 转换wiki字符串为HTML文本;

*/

public function parseWikiString (wikiString:String):String {

// 粗体处理

var result:String = parseBold(wikiString);

// 斜体处理

result = parseItalic(result);

// 段落处理

result = linesToParagraphs(result);

// 项目符号处理

result = parseBullets(result);

return result;

}

/**

* 粗体处理:将'''foo'''转换成<b>foo</b>

*/

private function parseBold(input:String):String {

var pattern:RegExp = /'''(.*?)'''/g;
// 有限匹配;

return input.replace(pattern, "<b>$1</b>");

}

/**

* 斜体处理:将''foo''转换成<i>foo</i>;

*/

private function parseItalic(input:String):String {

var pattern:RegExp = /''(.*?)''/g;

return input.replace(pattern, "<i>$1</i>");

}

/**

* 项目符号;将* foo转换成<li>foo</li>;

*/

private function parseBullets(input:String):String {

var pattern:RegExp = /^\*(.*)/gm;

return input.replace(pattern, "<li>$1</li>");

}

/**

* 段落处理:使用<p>HTML标签替换空行;

*/

private function linesToParagraphs(input:String):String {

/**

* Strips out(剔除) empty lines(空行), which match /^$/gm

*/

var pattern:RegExp = /^$/gm;

var result:String = input.replace(pattern, "");

/**

* 除了项目列表项外,其他都加上<P>标签;

*/

pattern = /^([^*].*)$/gm;

return result.replace(pattern, "<p>$1</p>");

}

}

}

=>URLParser.as

package com.cen.programmingas3.wikiEditor

{

/**

* 实用类:转换URL字符串

* _ such as "http://www.adobe.com" to HTML anchor links, such as "<a href='http://www.adobe.com'>http://www.adobe.com</a>;

*

* 特别说明:在此说明的例子中,url以及ftp正则表达式不是正规、严谨的,这里只是便于说明;

*/

public class URLParser

{

/**

* Converts HTTP and FTP URLs to anchor links. This function assembles(组装) a

* RegExp pattern out of multiple parts: protocol(协议), urlPart, and optionalUrlPart.

*/

public static function urlToATag(input:String):String {

/**

* 协议部分_http://

* Matches either http:// or ftp://. (?: indicates that the interior group

* is not a capturing group.(不捕获组)

*/

var protocol:String = "((?:http|ftp)://)";

/**

* www.adobe

*/

var urlPart:String = "([a-z0-9_-]+\.[a-z0-9_-]+)";
// matches foo.example;

/**

* .com

*/

var optionalUrlPart:String = "(\.[a-z0-9_-]*)";

/**

* 组装正则表达式

* Assembles the pattern from its component parts.

*/

var urlPattern:RegExp = new RegExp (protocol + urlPart + optionalUrlPart, "ig");

/**

* Replaces matching URL strings with a replacement string. The call to

* the replace() method uses references to captured groups (such as $1)

* to assemble the replacement string.

*/

var result:String = input.replace(urlPattern, "<a href='$1$2$3'><u>$1$2$3</u></a>");// <u>添加下划线;

/**

* Next, find e-mail patterns and replace them with <a> hyperlinks.

* 转换邮件地址

*/

result = emailToATag(result);

return result;

}

/**

* 如:mailto:bob@example.com;

* Replaces an e-mail pattern with a corresponding(相对应的) HTML anchor hyperlink.

* Like the urlToATag() method, this method assembles a regular expression out of constituent(组成) parts.

*/

public static function emailToATag(input:String):String {

/**

* mailto:

*/

var protocol:String = "(mailto:)"; // $1;

/**

* 姓名

* Matches the name and @ symbol, such as bob.fooman@.

*/

var name:String = "([a-z0-9_-]+(?:\.[a-z0-9_-])*@)";// $2;

/**

* For the e-mail pattern bob.fooman@mail.example.com, matches

* mail.example. (including the trailing dot).

*/

var domain:String = "((?:[a-z0-9_-].)*)";// $3;

/**

* Matches the superdomain, such as com, uk, or org., which is 2 - 4 letters.

*/

var superDomain:String = "([a-z]{2,4})";// $4;

/**

* Assembles(组装) the matching regular expression out of constituent parts.

*/

var emailPattern:RegExp = new RegExp (protocol + name + domain + superDomain, "ig");

/**

* Replaces matching e-mail strings with a replacement string. The call to

* the replace() method uses references to captured groups (such as $1)

* to assemble the replacement string.

*/

var result:String = input.replace(emailPattern, "<a href='$1$2$3$4'><u>$1$2$3$4</u></a>");

return result;

}

}

}

=>CurrencyConverter .as

package com.cen.programmingas3.wikiEditor

{

/**

* 美元转换成欧元类

*/

public class CurrencyConverter {

/**

* 美元转换成欧元

* Converts strings of US dollar values (such as "$9.95")

* to Euro strings (such as "8.24 €".

*/

public static function usdToEuro(input:String):String {

/**

* 美元正则表达式模式

*/

var usdPrice:RegExp = /\$([\d][\d,]*\.\d+)/g;

/**

* Replaces the matching dollar strings with the Euro equivalent string.

* The second parameter defines a function, used to define the

* replacement string.

*/

return input.replace(usdPrice, usdStrToEuroStr);

}

/**

* 如果replace()方法第二个参数:为函数,则会向其传递如下参数:

* - (1)、匹配部分:The matching portion(部分) of the string, such as "$9.95";

* - (2)、括号匹配项:The parenthetical(括号) match, such as "9.95";

* - (3)、匹配开始下标:The index position in the string where the match begins;

* - (4)、原始字符串:The complete string;

*

* This method takes the second parameter (args[1]), converts it to

* a number, and then converts it to a Euro string, by applying a

* conversion factor and then appending the € character.

*/

private static function usdStrToEuroStr(...args):String {

// 美元数值:

var usd:String = args[1];

// 去除逗号:

usd = usd.replace(",", "");

// 美元转换成欧元汇率:

var exchangeRate:Number = 0.828017;

// 欧元值:

var euro:Number = Number(usd) * exchangeRate;

// 欧元符号:

const euroSymbol:String = String.fromCharCode(8364);

return euro.toFixed(2) + " " + euroSymbol;

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: