(原创)怎样在word,windows live writer,或者博客(如网易博客,新浪博客)中设置代码语法高亮和底纹

  以往写的博客基本上每一篇都有一篇word版的备份,再通过windows live writer发布到博客中。技术博客中难免会有代码,但除了CSDN和cnblogs博客外,其它的如新浪,网易等对代码的高亮显示和底纹都是不支持的,插入的代码很难看。而且在使用windows live writer在这些平台上发布博客时,代码高亮也需要插件,本文提出了一种无需插件即可实现代码高亮显示的方法。

1.       首先在word中把要插入到博客中的需要高亮显示代码复制到Notepad++中。

2.       Notepad++中,设置Setting à Style Configurator… 在跳出的Style Configurator对话框中设置Select themeDefault(style.xml)(如果本来就是选择的defaultstyle,可跳过此步)。

3.       回到Notepad++的主窗口,在Language菜单中选择已经粘贴到Notepad++中的需要高亮显示的代码的种类,对于我们来说,主要是CC++VerilogPerl等。

4.       Plugins à NppExport à Copy RTF to clipboard。这时rtf格式的代码就已经存放到剪切板了。

5.       再回到word中,用剪切板中的rtf格式的代码替换掉要处理的代码,这时的代码已经可以高亮显示了,但是为了使代码更加美观,需用对它进行进一步的处理。主要包括:

a)         把段落字符转换成换行符。

b)        设置行距到最小,使代码更紧凑。

c)        设置代码底色

d)        设置边框和底纹

6.       可以使用下面的宏来对代码进行设置,首先在word中选择 工具àà宏,创建一个宏,把下面的代码贴入。之后,选中待选的代码,工具àà宏,运行刚才创建的宏。

Sub Macro1()
'
' Macro1 Macro
' ºêÔÚ 2011-5-4 ÓÉ hlren ¼ÖÆ
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
   
With Selection.Find
        .Text =
"^p"
        .Replacement.Text =
"^l"
        .Forward =
True
        .Wrap = wdFindAsk
        .Format =
False
        .MatchCase =
False
        .MatchWholeWord =
False
        .MatchByte =
True
        .MatchAllWordForms =
False
        .MatchSoundsLike =
False
        .MatchWildcards =
False
        .MatchFuzzy =
False
   
EndWith
    Selection.Find.Execute Replace:=wdReplaceAll
   
With Selection.ParagraphFormat
        .LeftIndent = CentimetersToPoints(
0)
        .RightIndent = CentimetersToPoints(
0)
        .SpaceBefore =
0
        .SpaceBeforeAuto =
False
        .SpaceAfter =
0
        .SpaceAfterAuto =
False
        .LineSpacingRule = wdLineSpaceAtLeast
        .LineSpacing =
1
        .Alignment = wdAlignParagraphJustify
        .WidowControl =
False
        .KeepWithNext =
False
        .KeepTogether =
False
        .PageBreakBefore =
False
        .NoLineNumber =
False
        .Hyphenation =
True
        .FirstLineIndent = CentimetersToPoints(
0)
        .OutlineLevel = wdOutlineLevelBodyText
        .CharacterUnitLeftIndent =
0
        .CharacterUnitRightIndent =
0
        .CharacterUnitFirstLineIndent =
0
        .LineUnitBefore =
0
        .LineUnitAfter =
0
        .AutoAdjustRightIndent =
True
        .DisableLineHeightGrid =
False
        .FarEastLineBreakControl =
True
        .WordWrap =
True
        .HangingPunctuation =
True
        .HalfWidthPunctuationOnTopOfLine =
False
        .AddSpaceBetweenFarEastAndAlpha =
True
        .AddSpaceBetweenFarEastAndDigit =
True
        .BaseLineAlignment = wdBaselineAlignAuto
   
EndWith
    Options.DefaultHighlightColorIndex = wdNoHighlight
    Selection.Range.HighlightColorIndex = wdNoHighlight
   
With Selection.ParagraphFormat
       
With .Shading
            .Texture = wdTextureNone
            .ForegroundPatternColor = wdColorAutomatic
            .BackgroundPatternColor = wdColorGray15
       
EndWith
        .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
        .Borders(wdBorderRight).LineStyle = wdLineStyleNone
        .Borders(wdBorderTop).LineStyle = wdLineStyleNone
        .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
       
With .Borders
            .DistanceFromTop =
1
            .DistanceFromLeft =
4
            .DistanceFromBottom =
1
            .DistanceFromRight =
4
            .Shadow =
False
       
EndWith
   
EndWith
   
With Options
        .DefaultBorderLineStyle = wdLineStyleSingle
        .DefaultBorderLineWidth = wdLineWidth050pt
        .DefaultBorderColor = wdColorAutomatic
   
EndWith
EndSub

7.       选中待处理的代码,设置行距为最小值,0磅。这一点也是我不明白的地方:在宏处理的时候设置成0磅,执行宏的时候会报错,说至少是1磅,而在word中手动修改则可以。

 

posted @ 2011-05-04 21:30  任怀鲁  阅读(2927)  评论(6编辑  收藏  举报