Hi clorr, hope we will find a good solution for this ;)
However the major drawback I see and that I tried to avoid, is that at each save, we may have concurrent access to the main file.
That's why in the 3rd step, the first thing to do is create a full page lock. This is not explained in my post but I was thinking that for getting a full page lock, the first thing is to check if there is already one, so if it's already locked, wait your turn. The full page lock shall mean you cannot access the temp file either. So I'm not worried about concurrent access to the main file.
And a second drawback is that we have to parse the main file with search and replace, which is time consuming.
This is very true. I don't know how bad could this be for performance, but I think that this would be an issue only under circumstances of many concurrent editing and frequent saves. We should benchmark how much time does it take to perform this.
Anyway, page renders such as made with the preview function are more time consuming because they are much more complicated. They imply much more markup and condition handling and probably may regular expressions are involved. For replacing the section chunks in the temp file, I would rather use search & replace functions that don't use regular expressions, so the regular expression engine doesn't have to be loaded, and the replacement should be quite fast.
I mean, I think this search & replace of an edited section would take place in a fraction of the time than for example syntax highlighting.