Wave and say goodbye to the csdn shit code box

Source: Internet
Author: User

Wave and say goodbye to the csdn shit code box

 

Wave and say goodbye to the csdn shit code box
Introduction

In all fairness, csdn is the most scum of the Technical Blog system I have ever seen. The scum lies in:

  • Mozilla Firefox does not support Pasting and copying.
    This is a programmer's technical blog website? Does it support open-source custom browsers? And the website owner thinks that the website is full of brainstorms and listens to what they say,
    Don't you want to do DIY plug-ins yourself?
  • The Code frame is like a moukeng stone, which is both bad and hard.
    There are few languages to support, so forget it. It's just a bit cool,
    You don't need the stuff that it provided in advance. It just drops your own formatted code. Use it, empty line,
    And text spacing, won't let you adjust. There are many comprehensive web page code highlighting systems on the Internet, such as geshi.
    Or free software,
    It's a pity that we can't do it for years.
  • Formatting of mathematical formulas is not supported at all.
    OK, then Google the keyword csdn again. This is the headline: csdn. net-the largest Chinese IT community in the world, providing IT professionals with the most comprehensive information !!.......

This is a pitfall !!!!!
Previously, the author wrote the Haskell module's custom installation configuration.
Emacs org-Mode
Add ditaa

Draw 1

The time spent is not as long as the time spent on transferring the format to csdn. So, if you want to write something,
There must be a method that can solve the formatting problem once and for all. It is the only solution that is perfectly compatible with csdn's current shit script...
Now, the problem is to use existing tools to automatically export the code in the required format and convert it to an image. What you need to ensure is that,
The indentation and format must be consistent between the exported code. As a DIY enthusiast, this export solution must be flexible enough.

1.1
Existing tools

There is no doubt that org-mode in Emacs
Its export functions are very powerful, and it will be even more powerful in the future with Babel.

, Similar to office
Cell computing and other functions are available in the face of Babel + Emacs, but in pediatrics, Babel can run your code inserted in plain text in real time,
Insert the final result to your document, which can be an image or a data table, and supports a wide variety of languages (almost all mainstream scripts and numerical computing software ).

Hi, these are both beautiful and beautiful wishes, at org-Mode
As long as you export the code segment directly,
You can generate beautiful HTML documents highlighted by code, .....

Useless, ordinary people, any effort before the csdn slag system is futile.
In front of the sigh wall in the code box, let's shed tears of remorse and despair ....
------ By csdn website architect
1.2
Ideal solution

Well, the article is still written in plain text. With the org-mode of Emacs, we need to do the following:

  1. Analyze the. org text, find the code segment of the next annotation, and put it into a temp buffer of Emacs.
  2. Use a programming language-specific syntax parser to classify expressions in the Code and perform
    Format annotation.
  3. Call the Tex tool set to generate a. DVI file and use the dvipng tool to generate a PNG image.
  4. Unregister the code segment in the original org file, insert the image link, and re-Execute Step 1 until the output code segment is not marked.
  5. Export the entire org file to generate the corresponding HTML code.

2
Work

To do this from scratch, the content is not simple. Fortunately, there are ready-made tools to complete most of the content. All you need to do is the glue code that connects the tools.

2.1
Additional tools required
Syntax analysis
Undoubtedly, the most difficult part is the syntax parser of the programming language itself, but this part does not need to be written by yourself (it is not a small project to write it), Haskell
The implementation itself is a hot topic in programming academia, So Haskell
Specialized official lexical analysis modules, Haskell-Src

& Haskell-Src-ext

And is built based on this for Haskell academic articles published Lhs2tex

To make it easier to color keywords, Lhs2TeX-hl

It's also a good thing 3

.
Format output
You don't have to think about it. Of course it's ---- word !!!...... Joke, according to fully loaded B Guide
As a programmer, how can you let wxxd words come out of your mouth? No, you can't even think about it. You need to format the layout, of course, latex.
It's King.
Template
That's right. Of course this is required !! Writing code should be like M $ V $ tudio. Open the editor, and the IDE automatically fills in a lot of code. For me, I will only go through Emacs lisp reference manual one day.
This is a must !! Fortunately, this template is very easy to find, just refer to the ob-haskell.el Haskell LHS export function (Org-Babel-Haskell-export-to-LHS) to do the rough work, on the basis of processing.
2.2
Lisp code

; Haskell-export.el --- <br/>; Copyright (c) 2011 Free Software Foundation, Inc. <br/>; Author: boyun Tang <tangboyun@gmail.com> <br/>; maintainer: boyun Tang <tangboyun@gmail.com> <br/>; created: 04 Mar 2011 <br/>; version: 0.01 <br/>; keywords <br/>; this program is free software; you can redistribute it and/or modify <br/>; it under the terms of the GNU General Public License S published by <br/>; the Free Software Foundation; either version 2, or (at your option) <br/>; any later version. <br/>; this program is distributed in the hope that it will be useful, <br/>; but without any warranty; without even the implied warranty of <br/>; merchantability or fitness for a particle purpose. see the <br/>; GNU General Public License for more details. <br/>; <B R/>; You shocould have got Ed a copy of the GNU General Public License <br/>; along with this program; if not, write to the Free Software <br/>; Foundation, inc ., 675 Mass Ave, Cambridge, MA 02139, USA. <br/>; commentary: <br/>; put this file into your load-path and the following into your ~ /. Emacs: <br/>; (require 'haskell-export) <br/>; Code: <br/> (eval-when-compile <br/> (require 'cl) <br/> (require 'ob-Haskell) <br/> (defun single-LHS-Export (& optional Arg) <br/> "export to. LHS file with all Haskell code blocks escaped. <br/>. LHS file will be exported to. tex file. this function will <br/> create two new files, base-name.lhs and base-name.tex and finally <br/> Export Result to base-name.png. note that input shoshould in Standard <br/> Haskell literate programming format. "<br/> (interactive" p ") <br/> (let * (<br/> ;; various file names to be used <br/> (base-Name (file-name-sans-extension (buffer-file-name ))) <br/> (TMP-file (make-temp-file "mytemp") <br/> (TMP-org-file (Concat TMP-File ". org ") <br/> (TMP-Tex-file (Concat TMP-File ". tex ") <br/> (LHS-file (Concat base-name ". LHS ") <Br/> (Tex-file (Concat base-name ". tex ") <br/> (FMT-file (Concat base-name ". FMT ") <br/> (DVI-file (Concat base-name ". DVI ") <br/> (PNG-file (Concat base-name ". PNG ") <br/>; Various tool software to be executed in shell, and its command parameters <br/> (PNG-control (Concat "-BG" "Transparent" <br/> "-d" "96" <br/> )) <br/> (lhs2tex "~ /. Cabal/bin // lhs2tex ") <br/> (texi2dvi (Concat "Latex" <br/> "<br/>"-o "DVI-file <br/>" <br/> (File -truename Tex-file ))) <br/> (dvipng (Concat "dvipng" <br/> "" <br/> PNG-Control <br/> "" <br/> DVI-file )) <br/> (lhs2tex (Concat lhs2tex "" LHS-file ">" Tex-file) <br/> (lhs2tex-hl (Concat "~ /. Cabal/bin/lhs2TeX-hl ""-o "<br/>" "FMT-file" "LHS-file )) <br/> (preserve-indentp org-Src-preserve-indentation) <br/> (indentation 0 )) </P> <p> (Org-Babel-mark-block); Use the interface provided by Babel, get all the code of the current SRC block <br/> (setq contents (buffer-substring-no-properties (region-beginning) (region-end ))) <br/> (with-temp-file TMP-org-file; create a new buffer and write all the function results to TMP-org-file <br/> (insert ;; the following is a small hacking, <br/ >;; If no additional title is added, the latex result in the last format will have a nested error <br/>; the solution is to insert an additional section first, and then remove this part with a regular expression. <Br/> (Concat "/N * tagforhaskell/n # + begin_latex/n" <br/> contents <br/> "/n # + end_latex /n "<br/>) <br/>) <br/> (save-excursion <br/>; this part exports the intermediate LHS file. <Br/> (find-file TMP-org-file) (funcall 'org-export-as-latex nil) <br/> (kill-buffer) <br/> (delete-file TMP-org-file) <br/> (find-file TMP-Tex-file) <br/> (goto-CHAR (point-min) (Forward-line 2) <br/>; the following defines the file header of A formatted Tex file, this part should be taken out and made another variable. <Br/>; put together for compact purposes. <Br/> (insert (Concat "// usepackage {xcolor}/N" <br/> "// usepackage [Top = 0.2in, Bottom = 0.2in, Left = 0.1in, right = 0.1in] {geometry}/N "<br/>" % include lhs2tex. FMT/N "<br/>" % include "FMT-file"/N "<br/>" // definecolor {syntax} {RGB} {0, 0,255}/N "<br/>" // definecolor {datatype} {RGB} {0,204, 0}/N "<br/>" // definecolor {class} {RGB} {239,181, 24}/N "<br/>" // definecolor {fieldname} {RGB} {162,}/N "<br/>" // Defin Ecolor {prelude} {RGB} {64, 80, 117}/N "<br/>" // definecolor {numeral} {RGB} {0, 0, 153}/N "<br/>" // definecolor {function} {RGB} {0, 0}/N "<br/>" // definecolor {infixoperator} {RGB} {215,106,175}/N "<br/>" // definecolor {constructor} {RGB }{ 229,120, 0}/N "<br/>" // definecolor {keyword} {RGB} {255, 0, 0}/N "<br/>" // definecolor {special1} {RGB} {159,138, 0}/N "<br/>" // definecolor {string} {RGB} {102, 51, 00}/N "<br/>" // define Color {Char} {RGB} {204,153, 0}/N "<br/>" // definecolor {comment} {RGB} {164,164,164}/N "<br/>" % SUBST conid A =/"// conid {// color {constructor} {/"A"/"}}/"/N "<br/>" % SUBST char a =/"// color {char} // text {// TT ''/"/"''}/"/N" <br/> "% SUBST string a = /" // color {string} // text {// TT // char34/"/" // char34}/"/N" <br/> "% SUBST numeral A =/"// color {numeral} {/" A "/"}/"/N" <br/> "// n Ewcommand {// lhschclass} [1] {// color {class }{# 1 }}/ N "<br/>" // newcommand {// lhschsyntax }[ 1] {// color {syntax }{{# 1 }}/ N "<br/>" // newcommand {// lhschfunction} [1] {// color {function }{{# 1 }}/ N "<br/>" // newcommand {// lhschinfixoperator} [1] {// color {infixoperator }{{# 1} }/N "<br/>" // newcommand {// lhschprelude} [1] {// color {prelude} {// mathbf {#1}/n "<br/>" // newcommand {// lhschkeyword} [1] {// color {keyword} {// textbf {#1 }}/ N "<br/>" // newcommand {// lhschconstructor} [1] {// color {constructor} {// textbf {#1} }/N "<br/>" // newcommand {// lhschlitnumber} [1] {// color {numeral} {#1 }}/ N "<br /> "// newcommand {// lhschtype} [1] {// color {datatype} {#1 }}/ N" <br/> "// newcommand {// lhscomment} [1] {// textit {// color {comment} {{# 1 }}}/N "<br/>" // pagestyle {empty} /n ")) <br/>; the following function is manual hack to correct nested issues of the generated LHS file. <Br/> (delete-extra-Info) <br/> (setq contents (buffer-string) <br/> (save-buffer) (kill-buffer )) <br/> (delete-file TMP-Tex-file) <br/>; save org exported latex to. LHS file <br/> (with-temp-file LHS-file (insert contents) <br/> (message "Running % s" lhs2tex-hl) <br/> (shell-command lhs2tex-hl) <br/> (message "Running % s" lhs2tex) <br/> (fix-Hl-bug FMT-file) <br/> (shell-command lhs2tex) <br/> (shell-command texi2dvi) <br/> (shell-command dvipng) <br/> )) <br/> (defun fix-Hl-bug (FMT) <br/> (find-file FMT) <br/> (goto-CHAR (point-min )) <br/> (replace-Regexp "^ % format // [//]. */N ") <br/> (save-buffer) </P> <p> (defun Delete-extra-Info () <br/> ;; delete unnecessary titles. We do not need to generate formal documents. <br/> ;; only the code snippet <br/> (setq tag-Reg "^ // Title //(. */N //) * // label. */N ") <br/> (goto-CHAR (point-min) <br/> (replace-Regexp tag-Reg "") <br/> (goto-CHAR (point-min) <br/>; When Babel automatically exports the LHS file, generate an additional unneeded/begin {code} And/end {code} nesting <br/> (replace-Regexp "^ // begin {document}/n +/ /// begin {code}/n + //((. */n) + //) /// end {code}/n + // end {document} "<br/>" ^ // begin {document}/n/1 // // end {document }")) <br/> (provide 'haskell-export) <br/>; haskell-export.el ends here <br/>

3
Final Effect

# + Begin_src Haskell <br/> you can insert any valid/latex {} code here // <br/>/lhscomment {<br/> O (n ). 'length' returns the length of a finite list as an 'int '. // <br/> it is an instance of the more general 'data. list. genericlength', // <br/> the result type of which may be any kind of number .} <br/>/begin {code} <br/> length :: [a]-> int <br/> length L = Len l 0 <br/> where <br/> Len :: [a]-> int <br/> Len [] A = A <br/> Len (_: XS) A = Len Xs (a + 1) <br/>/end {code} <br/> you can insert any valid/latex {} code here // <br/>/lhscomment {<br/> applied to predicate and a list, returns the list of those elements // <br/> that satisfy the predicate; I. E ., // <br/> filter P xs = [x $/vert $ x <-xs, p x]} <br/>/begin {code} <br/> filter:: (A-> bool)-> [a]-> [a] <br/> filter _ Pred [] = [] <br/> filter PRED (X: XS) <br/> | Pred x = X: filter Pred Xs <br/> | otherwise = filter Pred Xs <br/>/end {code} <br/> you can insert any valid/latex {} code here/< br/>/begin {code} <br/> class monad M => monadplus m where <br/> mzero:: m a <br/> mplus :: m a-> m a <br/> instance monadplus [] Where <br/> mzero = [] <br/> mplus = (++) <br/> instance monadplus maybe where <br/> mzero = nothing <br/> nothing 'mplus 'ys = ys <br/> Xs 'mplus' _ ys = Xs <br /> foldm:: (monad m) => (a-> B-> M) -> A-> [B]-> M A <br/> foldm _ A [] = return a <br/> foldm F A (x: XS) = f a x >=/ fax-> foldm F Fax Xs <br/>/end {code} <br/>#+ end_src <br/>

One-click Automatic Generation of the above Code: Note: The above haskell-export.el, currently only defines the function to capture a single code box, and has not been written to the temporary file cleaning function, is not a mature module, because latex and texi2dvi tools seem to still have a path search bug in Shell calls, this problem has not yet been resolved, so those files are saved with debug. The lhs2TeX-hl and lhs2tex themselves conflict when formatting. Mature modules and function optimization work should be done after these problems are solved. Ideally, you should be able to create a special custom tag in org-mode: # + begin_myworld ...... # + end_myworld: You can mix latex and code block at will, and finally export them to transparent images.

The advantage of the above tool is that you can mix latex and Haskell original code casually, including inserting the formatting of various mathematical formulas. Of course, the main reason is that Haskell supports literate programming itself.

Footnotes:

1

Thanks to the org-mode series of articles on the Emacs Chinese network.
To benefit me a lot.

2
Babel
Please do not miss out on the wonderful diyer website.

3

The last message of the author's git repository is: added missing file. again ..., Well, this module was written by an interesting guy. The most important module was missing after git was submitted. I caught this little braid and got it live.☻.

Author: boyun Tang

Date: 22:14:31 CST

HTML generated by org-mode 7.4 In Emacs 23

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.