【问题】
最新刚更新了Win7,包括升级IE9到IE10。
但是却发现,原来在WLW(Windows Live Writer)中正常工作的,我自己写的插件:picturemanager却不工作了:
无法正常的检索WLW中插入的本地图片,并替换对应的图片地址了。
且会导致WLW死掉。
【解决过程】
1.看来还得去专门调试一把。
2.调试发现,原来之前的,正则表达式:
string imgP = @"<A\s+?(?<wholeHref>href=""file:///(?<hrefFullname>(?<hrefPath>.+?WindowsLiveWriter-?\d+/supfiles\w+/).+?(?<pointSuf>\.\w{3,4}))""(?<spaceTarget>\s+?target="".+?"")?).*?>.+?(?<titleStr>title=""?(?<title>[^""]+)""?)\s+(border=.+?\s+)?(?<altStr>alt=""?\k<title>""?)\s+src=""file:///(?<srcFullname>\k<hrefPath>(?<srcFilename>.+?)\k<pointSuf>)"".+?(?<widthHeight>width=(?<width>\d+)\s+?height=(?<height>\d+)).*?></A>"; Regex imgRx = new Regex(imgP, RegexOptions.Singleline | RegexOptions.IgnoreCase);
对于如下的html:
<P><A href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/reply-for-use-a-record_thumb.png" target=_blank><IMG title="reply for use a record" style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BACKGROUND-IMAGE: none; BORDER-BOTTOM-WIDTH: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; DISPLAY: inline; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px" border=0 alt="reply for use a record" src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/reply-for-use-a-record_thumb.png" width=640 height=384></A></P> <P><A href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/use-80-port-to-login-hawk-cpanel2.png"><IMG title="use 80 port to login hawk cpanel" style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BACKGROUND-IMAGE: none; BORDER-BOTTOM-WIDTH: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; DISPLAY: inline; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px" border=0 alt="use 80 port to login hawk cpanel" src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/use-80-port-to-login-hawk-cpanel_thu.png" width=712 height=699></A></P>
是不匹配的,所以搜不到图片,无法实现地址替换了。
对应的原因是:
之前的情况是:style是在title之前
现在的情况变成了:style变到title之后了
所以之前的正则失效了。
然后看着如此复杂的正则,实在效率很低。
即使此处改正确了,等以后万一再变化一点点,则还是会出错。
所以,想办法换成之前所用的html的解析库:HtmlAgilityPack。
3.后来都写好了完整的代码了:
private void commonProcessFunction() { processedContent = curSelectCotent; //special is not thumb: //after replace: //<P><A //href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter2049324412/supfilesC93787/select-1st-cell-can-show-or-hidden_t.png" target="_blank"><IMG //style="BACKGROUND-IMAGE: none; BORDER-RIGHT-WIDTH: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; PADDING-TOP: 0px" //title="select 1st cell can show or hidden" border=0 //alt="select 1st cell can show or hidden" //src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter2049324412/supfilesC93787/select-1st-cell-can-show-or-hidden_t.png" //width=414 height=1204></A></P> //<P><A //href="file:///C:/Users/Administrator/AppData/Local/Temp/WindowsLiveWriter-1642303840/supfiles360FE7B/笑抽了 - 重口味的1楼和具有舍身精神的4楼[2].jpg"><IMG //style="BACKGROUND-IMAGE: none; BORDER-RIGHT-WIDTH: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; DISPLAY: inline; BORDER-TOP-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; PADDING-TOP: 0px" //title="笑抽了 - 重口味的1楼和具有舍身精神的4楼" border=0 alt="笑抽了 - 重口味的1楼和具有舍身精神的4楼" //src="file:///C:/Users/Administrator/AppData/Local/Temp/WindowsLiveWriter-1642303840/supfiles360FE7B/笑抽了 - 重口味的1楼和具有舍身精神的4楼_thumb.jpg" //width=607 height=803></A></P> //no border: //<P><A //href="file:///C:/Users/Administrator/AppData/Local/Temp/WindowsLiveWriter-1642303840/supfiles383DC9A/监控拍下的小偷:大姐,您这一箱啤酒放哪儿了?[2].gif"><IMG //style="DISPLAY: inline" title=监控拍下的小偷:大姐,您这一箱啤酒放哪儿了? alt=监控拍下的小偷:大姐,您这一箱啤酒放哪儿了? //src="file:///C:/Users/Administrator/AppData/Local/Temp/WindowsLiveWriter-1642303840/supfiles383DC9A/监控拍下的小偷:大姐,您这一箱啤酒放哪儿了?_thumb.gif" //width=234 height=165></A> </P> //<P><A //href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/reply-for-use-a-record_thumb.png" //target=_blank><IMG title="reply for use a record" //style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BACKGROUND-IMAGE: none; BORDER-BOTTOM-WIDTH: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; DISPLAY: inline; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px" //border=0 alt="reply for use a record" //src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/reply-for-use-a-record_thumb.png" //width=640 height=384></A></P> //<P><A //href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/use-80-port-to-login-hawk-cpanel2.png"><IMG //title="use 80 port to login hawk cpanel" //style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BACKGROUND-IMAGE: none; BORDER-BOTTOM-WIDTH: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; DISPLAY: inline; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px" //border=0 alt="use 80 port to login hawk cpanel" //src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfiles22CDBA/use-80-port-to-login-hawk-cpanel_thu.png" //width=712 height=699></A></P> HtmlAgilityPack.HtmlDocument htmlDoc = crl.htmlToHtmlDoc(processedContent); HtmlNode rootNode = htmlDoc.DocumentNode; HtmlNodeCollection aImgNodeList = rootNode.SelectNodes("//p/a[contains(@href, 'AppData/Local/Temp/WindowsLiveWriter-')]"); if (aImgNodeList != null) { foreach (HtmlNode aImgNode in aImgNodeList) { string originalAImgHtml = aImgNode.OuterHtml; string aHrefVal = aImgNode.Attributes["href"].Value; if (ckbTargetBlank.Checked) { //always set to target="_blank" aImgNode.SetAttributeValue("target", "_blank"); } HtmlNode imgNode = aImgNode.SelectSingleNode("./img"); if (imgNode != null) { string imgSrcVal = imgNode.Attributes["src"].Value; //the processed and uploaded picture, also here contains Local/Temp/WindowsLiveWriter- //<P><A //href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfilesC19F10/now-the-service-status-is-active_thu%5B1%5D.png" //target=_blank><IMG title="now the service status is active_thumb" //style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; MARGIN: 0px; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px" //border=0 alt="now the service status is active_thumb" //src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfilesC19F10/now-the-service-status-is-active_thu%5B1%5D.png" //width=640 height=452></A></P> //so need check whether href is same with src if (aHrefVal.Equals(imgSrcVal, StringComparison.OrdinalIgnoreCase)) { //already processed, not handle } else { if (ckbReplaceHrefWithSr.Checked) { aImgNode.SetAttributeValue("href", imgSrcVal); } } if (ckbAutoScale.Checked) { string realSrcFullname; string srcFilename; //string decodedSrcFilename; //used for translation int srcFileWidth, srcFileHeight; //int maxWidth = 640, maxHeight = 480; int maxWidth = 640; int newWidth, newHeight; string srcFullname = imgSrcVal; srcFilename = crl.extractFilenameFromUrl(imgSrcVal); //check file exist or not, and find out the real full filename and filename if (!System.IO.File.Exists(srcFullname)) { realSrcFullname = HttpUtility.UrlDecode(imgSrcVal); //decodedSrcFilename = HttpUtility.UrlDecode(srcFilename); } else { realSrcFullname = srcFullname; //decodedSrcFilename = srcFilename; } //remove file:///, otherwise will Image.FromFile will fail !!! string noFilePrefixRealSrcFullname = realSrcFullname; if (realSrcFullname.StartsWith("file:///")) { noFilePrefixRealSrcFullname = realSrcFullname.Substring(8); } Image img = Image.FromFile(noFilePrefixRealSrcFullname); //"C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfilesECB445/use-80-port-to-login-hawk-cpanel_thu.png" srcFileWidth = img.Width; srcFileHeight = img.Height; //here only scale according to width if (srcFileWidth > maxWidth) { newWidth = maxWidth; newHeight = newWidth * srcFileHeight / srcFileWidth; imgNode.SetAttributeValue("width", newWidth.ToString()); imgNode.SetAttributeValue("height", newHeight.ToString()); } //replace img html //originalAImgHtml = originalAImgHtml.Replace(imgNode.OuterHtml, imgNode.WriteTo()); } //do finnal replace string newAImgHtml = aImgNode.WriteTo(); processedContent = processedContent.Replace(originalAImgHtml, newAImgHtml); } else { //something wrong } } } }
结果却是:
对于originalAImgHtml,实际上和本身的html不同:
即原先的html是:
<A href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfilesFFB3D9/use-80-port-to-login-hawk-cpanel2.png"><IMG title="use 80 port to login hawk cpanel" style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BACKGROUND-IMAGE: none; BORDER-BOTTOM-WIDTH: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; DISPLAY: inline; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px" border=0 alt="use 80 port to login hawk cpanel" src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfilesFFB3D9/use-80-port-to-login-hawk-cpanel_thu.png" width=712 height=699></A>
而originalAImgHtml,即aImgNode.OuterHtml却是,处理后的:
<a href="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfilesFFB3D9/use-80-port-to-login-hawk-cpanel_thu.png" target="_blank"><img title="use 80 port to login hawk cpanel" style="BORDER-LEFT-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px; BACKGROUND-IMAGE: none; BORDER-BOTTOM-WIDTH: 0px; PADDING-TOP: 0px; PADDING-LEFT: 0px; DISPLAY: inline; PADDING-RIGHT: 0px; BORDER-TOP-WIDTH: 0px" border="0" alt="use 80 port to login hawk cpanel" src="file:///C:/Users/CLi/AppData/Local/Temp/WindowsLiveWriter-1737927945/supfilesFFB3D9/use-80-port-to-login-hawk-cpanel_thu.png" width="640" height="628"></a>
很明显,改变了:
标签的大小写
把一些属性,比如width和height的值,都加上了对应的引号
如此,导致:
虽然我们可以修改标签的各个属性值,且可以获得修改后的内容
但是由于最原始的originalAImgHtml,根本就不存在,所以
processedContent = processedContent.Replace(originalAImgHtml, newAImgHtml);
根本没有生效,所以导致我们此处的替换无效。
即,浪费了半天功夫,根本没有实现替换功能。
4.所以,看来还是只能换回原先的,用正则处理的了。
最后,为了匹配新的,style在boarder之后的,只是把:
string imgP = @"<A\s+?(?<wholeHref>href=""file:///(?<hrefFullname>(?<hrefPath>.+?WindowsLiveWriter-?\d+/supfiles\w+/).+?(?<pointSuf>\.\w{3,4}))""(?<spaceTarget>\s+?target="".+?"")?).*?>.+?(?<titleStr>title=""?(?<title>[^""]+)""?)\s+(border=.+?\s+)?(?<altStr>alt=""?\k<title>""?)\s+src=""file:///(?<srcFullname>\k<hrefPath>(?<srcFilename>.+?)\k<pointSuf>)"".+?(?<widthHeight>width=(?<width>\d+)\s+?height=(?<height>\d+)).*?></A>"; |
换为:
string imgP = @"<A\s+?(?<wholeHref>href=""file:///(?<hrefFullname>(?<hrefPath>.+?WindowsLiveWriter-?\d+/supfiles\w+/).+?(?<pointSuf>\.\w{3,4}))""(?<spaceTarget>\s+?target="".+?"")?).*?>.+?(?<titleStr>title=""?(?<title>[^""]+)""?)\s+.+?(border=.+?\s+)?(?<altStr>alt=""?\k<title>""?)\s+src=""file:///(?<srcFullname>\k<hrefPath>(?<srcFilename>.+?)\k<pointSuf>)"".+?(?<widthHeight>width=(?<width>\d+)\s+?height=(?<height>\d+)).*?></A>"; |
即可。
至此,目前至少还是可以继续使用了。
【总结】
一次普通的Win7的update,竟然会影响到到WLW,真是无语了。。。