The experience of finding the Vspaste plugin for Windows Live writer lost Rich Text format information at once

Source: Internet
Author: User
Tags blog layout

Background

I am blogging in the blog Park using Windows Live Writer, the code highlighting plugin is using paste from Visual Studio (hereinafter referred to as Vspaste).

Further information from Windows Live Writer can be summarized in the blog park using the "hyper-detailed tutorial", published articles in Windows Live Writer 2012 and office Word 2013, here.

Vspaste further information, can refer to Cnblogs blog layout skills. Because the end date of Windows Live Writer 2012 is January 10, 2017, and the corresponding plug-in site is also closed, so there is no official download, there is a need to contact me.

Cause

Long time no updated blog, one is lazy, two is nothing worth sharing. Just have a little to share on the topic, began to happily write a blog. Writing, found that the vspaste copy of the loss of the format of the situation, so to study the problem.

In the case of losing the format, for example, I copied the following code:

However, when you use Vspaste to insert into Windows Live Writer, the text is All Black. What about green and blue?

Check if Vspaste is wrong

Since Vspaste has not been updated for a long time, my first response is to see if Vspaste is wrong. In order to verify the judgment, we may as well establish a project for testing.

Find a Portal

Before you build your project, you need to understand the function portals that Windows Live writer calls Vspaste. Searching for Windows Live Writer plugin develop on Bing, and found an article called Developing Plugins for Windows Live Writer, learned to discover that Plugins must inherit from ContentSource or Smartcontentsource. Where ContentSource is inserted directly into Windows Live writer, the Smartcontentsource feature is richer, such as the ability to add post-compilation.

Open Ilspy, and drag the Vspaste assembly into it. After a simple review, it is found that the Vspaste plugin's entry class inherits from the Smartcontentsource. And it's simple to determine if there is a rich Text format data in the Clipboard, and if it does, convert it to HTML.

In addition, the official code coloring controls published by the blog Park Cnblogs.codehighlighter do, as they say, submit the code to the server for processing. Such as:

Fill Test Engineering

Through the analysis, we can build a simple test project. When analyzing the Vspaste portal, it was found to have referenced System.Windows.Forms. So we might as well create a new Windows Forms application to display the RTF before conversion and the converted HTML.

Add a reference to the vspaste after you create the project, and then add the required WindowsLive.Writer.Api for Vspaste. Where is this DLL? Because it is the Windows Live Writer plugin, guess is in the Windows Live Writer installation directory, a look, sure enough exists this DLL. But what if it doesn't exist in the installation directory? I prefer to use everything this software, you can directly enter the file name lookup, speed and fast. However, the use of the software is necessary to ensure that the corresponding disk is the NTFS file system.

Complete the main interface, a main interface consists of two text boxes, a two-line TableLayoutPanel and a button, as follows:

The response to the test button is as follows:

Run Failures and Solutions

Our test project is finished, so let's run it and try it out. Compile successfully, run successfully, next copy a piece of code in VS, click Run Try. Unfortunately, this error has occurred:

I have experience with this error, and it appears more in the P/invoke scene. It is also better to solve, in the project properties of the Build tab page to change the build platform to x86. OK, try again, still error:

Not science ah, usually can do ah, how this time there is a problem. In contrast, there is a difference, this time it is found that the assembly manifest definition does not match the assembly reference. Click to view more information, such as:

Read the Fusionlog information carefully and find that the assembly version of WINDOWSLIVE.WRITER.API should be inconsistent. Did I neglect it?

Open Ilspy and view the referenced WindowsLive.Writer.Api of Vspaste. Results such as:

Then, in Ilspy, look at the version information for WINDOWSLIVE.WRITER.API in the Windows Live Writer 2012 directory that I installed. Results such as:

Smart as you, must have found the above difference. Yes, the version referenced by Vspaste is 1.0.0.0, and Windows Live Writer uses 1.1.0.0, and the platform is x86. This also explains why the first run-time prompt attempts to load a program that is not properly formatted.

Now that you know the problem, it's easy to solve. You need to use assembly redirection at this time to specify the assembly bindings in the application configuration file, such as:

Run results

In VS, copy the first piece of code, run the program, and click the Test button:

The results of the operation are as follows. Rich Text Format I know it's not too deep, but it doesn't affect the next action. Create a new text document, copy the text from the upper text box to it, and change its extension to RTF. Open the file with the following effect:

It can be found that the RTF that was copied before the HTML was generated is not correct.

Run the results again

Emulate the code in WordPad, such as:

Copy the code in RTF, run the program, click the Test button:

The results of the operation are as follows. The following HTML does not look very intuitive, create a new text document, copy the text in the text box into it, and change its extension to HTML. Open the file with the following effect:

As you can see, vspaste is not wrong.

Further finding reasons

As you can see from the previous experiment, Vspaste does not make any mistakes, and the code copied from VS has lost rich Text format information. So where does the problem really appear? I used to use vspaste in VS2015 no problem. At this point I have a guess, if there is no problem with VS, then it is very likely that the plug-in pit dad.

Find the problem plugin

Select Tools--Extensions and updates in VS to open the plug-in list by using dichotomy to disable plugins to see if the problem is resolved (restart vs when disabled). Soon, found the culprit, is the following goods:

Find problem Features

Can we find the problem plug-in to this point? Of course. But for oneself, always want to break the casserole to ask the end. You can find more information about productivity Power Tools 2015 by clicking on the details in the right-hand side. You can learn about its various functions and also know that each function can be switched on.

Select Tools---options in VS and select productivity Power tools in the tree control on the left side of the form. Such as:

In the previous understanding of productivity Power tools, I already had the object of doubt, that is, HTML Copy. Try turning it off to see if the problem is resolved (disabling requires restarting VS). After the experiment, it is found that the problem is caused by this function.

Can you further find the source of the problem? The answer is yes. If you pay more attention to the details of productivity Power Tools 2015, you'll find a link to GitHub in the upper-right part of the page. Well, the project is still Microsoft's, and I don't know why it's such a problem.

Source Debugging download code

Download the code for productivity Power tools from GitHub, as it is a collection of plug-ins, and quickly find the corresponding HTML copy item.

Find the problem code

The code is not too much, you can take a file-by-document reading method. But I already know the symptoms, that is, the copy of the RTF data is not correct, then you might want to find the code where the Clipboard is used. It was soon found:

The function has only one reference, and you can find it by looking at the reference:

Then look at the definition of Generateclipboarddata:


To continue the search, you can see that both Htmlbuilderservice and Rtfbuilderservice are imported through MEF.


After the code that generates HTML and RTF, hit a breakpoint and start debugging. Open the project in a new running VS instance and copy the code. Viewing at a breakpoint, you can see that the generated RTF has lost formatting information, and the HTML still retains the formatting information.

So where is this rtfbuilderservice sacred? View details in the Watch window:

Realize your rtfbuilderservice.


Now that you know the class and the assembly that implements Rtfbuilderservice, open the assembly in Ilspy and navigate to the class:

Create a new class in the project, the class name cannot be Rtfbuilderservice, and all the code in the Ilspy is copied out into the class. The export type of the class is set to the corresponding class name, and the corresponding class is imported instead of the Irtfbuilderservice. As follows:

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

Analyze Rtfbuilderservice Code

Looking for the Generatertf method in the Rtfbuilderservice internal code we implemented, we found that it used the following method:


Then look at the Generatertf method inside the Rtfbuilder


First look at the Generatebody method and discover that it generates RTF mainly by analyzing the properties of the Textrunproperties:


The text attribute is derived from Getclassificationspans:

Debug Rtfbuilderservice

After the Generatebody method gets textrunproperties, hit a breakpoint and start debugging. Open the project in a new running VS instance and copy the code. The color of the Text property is viewed at the breakpoint, and it is found that only one breakpoint is entered, and the text foreground color is white and the background color is black.

Copy the code again and view the current property information in the Watch window. It has only the classificationtype and span properties. The span attribute can be seen as if it is a copy of the code we copied, try to see if there is a way to get the text, a look, and really:

By comparing the values of the Watch window, you can see that all the formatting information has been lost at this time. Because our classificationtype is empty, the default Text property is always returned.

In the first line of the Getclassificationspans hit a breakpoint, the single-step debugging, you can find some information. The Cancel argument that calls the method is empty, and Getclassificationspans returns no entries in the list, so the constructor of the Nullableclassificationspan with the empty Classificationtype parameter is always called. Next, based on a layer-by-level view of the call stack, it was found that cancel was determined at the beginning of the Generateclipboarddata method call Generatertf.

This can do, the clue is broken again, really is no way to go? No, we still have a way! This project does not have to generate HTML code, it is not lost format, to refer to the chant.

Reference code to generate HTML

After some searching, we found code similar to the Getclassificationspans method in Rtfbuilderservice in generating HTML code, even in the same name.

View the Getclassificationspanssync method called by this code:


It was found that the call Getallclassificationspans with a cancellationtoken parameter, and the iwaitindicator that generated waitcontext originated from the MEF import.

What's up, isn't it situations, Vista?

Perfect Rtfbuilderservice

Generate part of the leaf out according to the HTM, for example, the red part represents the new code:

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

Well, the problem has been solved. Is there anything else you can do?

Another solution

Let's go back to Rtfbuilderservice again, why does it have so many overloads?


Looking at the implemented interfaces, we found that the IRTFBUILDERSERVICE2 was implemented in addition to implementing irtfbuilderservice. Two-Interface definition comparison:


It is not hard to see that IRtfBuilderService2 is adding a cancellationtoken overload after each method Irtfbuilderservice.

So, instead of adding classes, we can change the imported irtfbuilderservice type to IRtfBuilderService2, and pass in the Canceltoken in the place where the RTF is generated to invoke the corresponding interface method.

Precautions

Microsoft recommends that productivity Power tools uninstall is required when debugging plugins. I was unloading the problem while I was working on it, but I didn't uninstall it when I wrote this blog, and it seemed like there was no problem.

In addition, because I was the first to study the question, after the blog, may have some details forgotten or did not write, the heart of the study can contact me.

Conclusion

Because things are more, intermittent so long, and finally finished writing this blog. The reason why write so much, mainly want to share my problem-solving process and ideas. As a programmer, I personally feel that there are some of the spirit of exploration better, many times, the road is not so difficult to imagine.

Finally, I gave Microsoft a issue ...

The experience of finding the Vspaste plugin for Windows Live writer lost Rich Text format information at once

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.