One time to find Windows Live Writer's VSPaste plug-in to lose the RTF format information, vspastertf

Source: Internet
Author: User

One time to find Windows Live Writer's VSPaste plug-in to lose the RTF format information, vspastertf
Background

I wrote a blog on the blog park using Windows Live Writer and the code highlight plug-in using Paste from Visual Studio (VSPaste ).

For more information about Windows Live Writer, refer to the [detailed tutorial] to use Windows Live Writer 2012 and Office Word 2013 to publish an article to the blog.

For more information about VSPaste, refer to the CnBlogs blog typographical skills. Because Windows Live Writer 2012 is terminated on July 15, January 10, 2017 and the corresponding plug-in website is closed, there is no official download. If you need it, contact me.

Cause

I haven't updated my blog for a long time. I'm lazy, and I have nothing to share. I just had a few topics to share and began to write blogs happily. It says, I found that VSPaste replication has a lost format, so I will study this problem.

For example, I copied the following code:

However, after being inserted into Windows Live Writer using VSPaste, all the text is black. What about green and blue?

Check whether VSPaste has an error

Because VSPaste has not been updated for a long time, my first response is to check whether VSPaste has an error. To verify the judgment, we may wish to establish a project for testing.

Search Portal

Before creating a project, you must first understand the function entry for Windows Live Writer to call VSPaste. Search for windows live writer plugin develop on Bing and find an article named Developing Plugins for Windows Live Writer. After understanding this article, we find that the plug-in must inherit from ContentSource or SmartContentSource. The ContentSource directly inserts HTML into Windows Live Writer, and the SmartContentSource function is richer, for example, it can be added and compiled.

Open ILSpy and drag the VSPaste assembly into it. After a simple view, we found that the entry class of the VSPaste plug-in inherits from SmartContentSource. In addition, it is easy to do. You can determine whether there is RTF data in the clipboard. If so, convert it to HTML.

In addition, CNBlogs. CodeHighlighter, a code coloring control officially released by the blog Park, submits the code to the server for processing as they said. For example:

Fill test project

Through analysis, we can establish a simple test project. When analyzing the VSPaste entry, we found that it references System. Windows. Forms. Therefore, we may create a new Windows form application to display the RTF before conversion and the HTML after conversion.

Add a reference to VSPaste after creating a project, and then add WindowsLive. Writer. Api required by VSPaste. Where is this DLL? Because it is a Windows Live Writer plug-in, it is assumed that the DLL exists in the installation directory of Windows Live Writer. But what if the installation directory does not exist? I prefer the Everything software. You can directly enter the file name to search for it, which is fast. However, the premise of using the software is to ensure that the corresponding disk is an NTFS file system.

Complete the main interface. A main interface consists of two text boxes, one TableLayoutPanel with two rows, and one button, as shown below:

The response of the test button is as follows:

Operation Failure and Solution

Our test project has been completed. Next we will try it. Compile successfully and run successfully. Next, copy a piece of code in VS and click Run. Unfortunately, this error occurs:

I have experience with this error, most of which occur in P/Invoke scenarios. The solution is also better. On the project property generation tab, change the generation platform to x86. Now, try again and still report the following error:

It's not scientific. You can do it normally. Why is there a problem this time. After careful comparison, there is a difference. The Assembly list definition found this time does not match the Assembly reference. Click to view details, such:

Read the FusionLog carefully and find that the Assembly version of WindowsLive. Writer. Api is inconsistent. Is it my negligence?

Open ILSpy to view the WindowsLive. Writer. Api referenced by VSPaste. The result is as follows:

In ILSpy, view the Windows Live. Writer. Api version under the Windows Live Writer 2012 directory that I have installed. The result is as follows:

If you are smart, you must have discovered the difference above. Yes, the version referenced by VSPaste is 1.0.0.0, while Windows Live Writer uses 1.1.0.0 and the platform is x86. This also explains why the first runtime prompts you to try to load a program with an incorrect format.

Now that you know the problem, it's easy to solve. In this case, you need to use assembly redirection to specify assembly binding in the application configuration file, such:

Running result

In VS, copy the code at the beginning, run the program, and click the test button:

The running result is as follows. I do not know much about the RTF format, but it does not affect the subsequent operations. Create a text document, copy the text in the upper text box to it, and change its extension to rtf. Open the file. The effect is as follows:

We can find that the copied RTF is incorrect before HTML is generated.

Run the result again.

Simulate the corresponding code in the WordPad. The effect is as follows:

Copy the code in RTF, run the program, and click the test button:

The running result is as follows. The following HTML does not seem intuitive. Create a text document, copy the text in the text box to it, and change its extension to html. Open the file. The effect is as follows:

We can see that there is no error in VSPaste.

Further find the cause

From the previous experiment, we can see that there is no error in VSPaste, and the code copied from VS has lost the RTF format information. So what exactly does the problem occur? I used VSPaste in VS2015. At this time, I guess that if VS has no problems, it is very likely that it is a plug-in.

Find problem plug-ins

In VS, choose tools> extensions and updates to open the plug-in list. Disable the plug-in using the binary method to check whether the problem is resolved (restart VS after disabling it ). Soon, the culprit was found:

Search for Problems

Can this problem be solved after we find the problem plug-in? Of course. But for myself, I always want to break the casserole to the end. Click Details on the right side of the page to learn more about Productivity Power Tools 2015. You can learn about its functions and switch each function.

Select Tools> options in VS, and select Productivity Power Tools in the tree control on the left of the form. For example:

In the previous sections on Productivity Power Tools, I have already suspected objects, that is, HTML Copy. Disable it to check whether the problem is resolved (restart VS after disabling it ). After testing, it was found that this feature caused problems.

Can I further find the root cause? The answer is yes. If you pay more attention to the details of Productivity Power Tools 2015, you will find a link to GitHub in the upper right part of the page. Well, the project is still Microsoft. I don't know why this problem occurs.

Source code debugging and downloading code

Download the Productivity Power Tools code from GitHub. As it is a collection of plug-ins, the corresponding project of HTML Copy is quickly found after the download.

Find problem code

There are not many codes, so you can read them one by one. But I already know the symptom, that is, the copied RTF data is incorrect, so you may wish to find the place where the clipboard is used in the code. Soon I found out:

This function has only one reference. You can find the reference:

Check the definition of GenerateClipboardData:


You can find that both htmlBuilderService and rtfBuilderService are imported through MEF.


After generating html and rtf code, open a breakpoint to start debugging. Open the project in the newly running VS instance and copy the code. View at the breakpoint and you can find that the generated rtf has lost the format information, while html still retains the format information.

So what is this rtfBuilderService? View details in the monitoring window:

Implement your own RtfBuilderService


We already know the class implementing rtfBuilderService and the Assembly. Open the Assembly in ILSpy and locate the class:

Create a new class in the project. The class name cannot be RtfBuilderService. Copy all the code in ILSpy and put it in the class. Set the export type of the class to the corresponding class name, and change the import to the corresponding class at the place where IRtfBuilderService is imported. As follows:

The reason for changing the class name is to avoid invalid MEF import and export. If it fails, the following situations may occur:

Analyze RtfBuilderService code

Find the GenerateRtf method in the internal code of our RtfBuilderService implementation and find that it uses the following method:


Check the GenerateRtf method in RtfBuilder.


First, check the GenerateBody method and find that it is mainly used to generate the rtf by analyzing the properties of TextRunProperties:


The text property comes from GetClassificationSpans:

Debug RtfBuilderService

After the GenerateBody method obtains TextRunProperties, open a breakpoint to start debugging. Open the project in the newly running VS instance and copy the code. View the color of the text attribute at the breakpoint. It is found that only one breakpoint is entered, the text foreground is white, and the background color is black.

Copy the code again to view the current attribute information in the monitoring window. Only ClassificationType and Span attributes are available. In the Span attribute, we can see that its content is like the code we copied. We try to see if there is any way to get the text. check whether there are:

Compare the values in the monitoring window to find that all the format information is lost. Because our ClassificationType is empty, the default text attribute is always returned.

You can click a breakpoint in the first line of GetClassificationSpans to perform one-step debugging and find some information. The cancel parameter for calling this method is null, while the list returned by GetClassificationSpans has no entries. Therefore, the constructor of NullableClassificationSpan with the ClassificationType parameter being null is always called. Next, based on the layer-by-layer view of the Call Stack, it is found that the cancel is empty when the GenerateClipboardData method calls GenerateRtf at the beginning.

What can I do? The clues are broken again. Is there no way to go? No, we have another path! Isn't this project generating HTML code? It's not without losing the format. Please refer to it.

Refer to generate HTML code

After some searching, we found that the code in the generated HTML code is similar to the function of the GetClassificationSpans method in RtfBuilderService, with the same name.

View the GetClassificationSpansSync method called by this Code:


It is found that this call to GetAllClassificationSpans carries a CancellationToken parameter, and the IWaitIndicator that generates WaitContext comes from MEF import.

How do you do?

Improve RtfBuilderService

Generate part of the image based on the HTM. For example, the red part indicates the new code:

After debugging and testing, it is found that the generated RTF already contains the correct format information.

Okay, the problem has been solved. Have we reached this point? What else can I do?

Another solution

Let's go back to RtfBuilderService again. Why is there so many reloads?


View the implemented interface and find that in addition to implementing IRtfBuilderService, IRtfBuilderService2 is also implemented. Comparison between the two interfaces:


IRtfBuilderService2 adds a CancellationToken overload to each method of IRtfBuilderService.

Therefore, we can directly change the imported IRtfBuilderService type to IRtfBuilderService2 without adding a new class, and input CancelToken to call the corresponding interface method in the generated rtf.

Notes

Microsoft recommends that Productivity Power Tools be uninstalled when debugging plug-ins. I uninstalled my research, but I didn't uninstall it when I was writing this blog. It seems that there is no problem.

In addition, because I studied the problem first, the blog I wrote later may have forgotten or failed to write some details. If you are interested in the research, contact me.

Conclusion

Because there are many things, this blog has finally been written out after such a long pause. The reason why I wrote so much is mainly to share my problem solving process and ideas. As a programmer, I personally think it is better to have some exploration spirit. In many cases, the road is not as difficult as I think.

Finally, I gave Microsoft an issue ......

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.