There are code examples on MSDN where formatting is broken:

For example, a multiline code can be written in one line => if there is a comment at the beginning of the code, then all the code is displayed as a comment ...

Each such example has to be copied to notepad ++ and corrected by hand.

Tell me, can this somehow be fixed?

I tried to open the standard IE and switch the language, but did not help ...

UPD

Example:

https://msdn.microsoft.com/ru-ru/library/dd460648(v=vs.110).aspx

private Program() { //An aggregate catalog that combines multiple catalogs var catalog = new AggregateCatalog(); //Adds all the parts found in the same assembly as the Program class catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly)); //Create the CompositionContainer with the parts in the catalog _container = new CompositionContainer(catalog); //Fill the imports of this object try { this._container.ComposeParts(this); } catch (CompositionException compositionException) { Console.WriteLine(compositionException.ToString()); } } 
  • one
    Although the links would be added to such a thing, otherwise I did not come across for example. - Monk
  • Strange, I only reproduce in Russian, on the English page everything is OK: msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx - Grundy
  • 3
    Judging by the fact that everything is fine on the original English formatting page, you need to say thanks to the automatic translator, who apparently tries to translate the code as well, and at the same time breaks the markup. - rdorn
  • Aha, the translator sobsn and suspected why asked. This, in my opinion, is normal, and there the text is sometimes indistinct, what to expect from the code. - Monk
  • I can give this link: LockBits . The sample code contains the characters . - Alexander Petrov

1 answer 1

There are at least two reasons for this. The first is that the code itself is published in a curved form. There's nothing you can do about it.

The second reason is a good example of why we shouldn't parse the code with regulars. On all sites, syntax highlighting is done with simple regulars (in the browser, you can’t do it otherwise). Total:

c #:

This does not work:

 string quote = @"\""; int x = 0; 

And it works (you need to explicitly specify the language <!-- language: lang-c# --> ):

 string quote = @"\""; int x = 0; 

xml:

 <root atr="value"> <узел атр="знач"/> </root> 

As you can see, even here, on stackoverflow, the backlight is broken in some cases.


I'm fantasizing. What can be done with this? The matter is complicated by the fact that forums usually publish pieces of code that are not complete in their essence. That is, the parser must understand not only the syntax, but also the semantics of the language, be able to do the restoration and continue parsing after the encountered error. Roslyn can already do this (c # compiler).

Therefore, syntax highlighting and formatting should be done by parsing the code with a full parser. For this you need a plugin (s) to the browser, sharpened for a specific language. Or, alternatively, the parsing is done on the server, and the ready markup is sent to the browser.

  • "There's nothing you can do about it." I wonder if there are plugins for the browser that format the code? - user2455111
  • one
    @ user2455111 - for sure, yes. However, if there is something, then there is a primitive analysis again regulars. Using a parser / compiler - have not heard of such. There is a chance to become a pioneer;) - Alexander Petrov
  • @AlexanderPetrov, I broke your example with c # maybe you should delete it :) - Grundy
  • @Grundy - Shaytan! However, if the c# tag is simply in question, then the highlighting is performed as in the original version. - Alexander Petrov
  • one
    @AlexanderPetrov I updated the answer to the meta-meta.ru.stackoverflow.com/a/4254/177221 . Your example has invalid C # code, int x = 0; is part of the string. and it is highlighted correctly. but the indication of lang-c# leads to a non-correct illumination :) - PashaPash