Posting Code in Blogs

Using “SyntaxHighlighter Evolved” in the WordPress environment to post code snippets.

How to post code:

  • Use WordPress web-based editor to write/edit the post
  • Switch to HTML editing mode (don’t switch back to Visual Mode, it will screw up some of the code and PRE tags)
  • Open the code with [sourcecode language=”csharp” gutter=”false”]
  • Close the code with [/sourcecode]

gutter=”false” means no line numbers.
can add highlight=”4,7″ to highlight lines 4 & 7 in the source block

Tips

  • Write your content first in LiveWriter (include pictures if you want, but no code). Get it all formatted, post it as a draft, and check it out in the browser. Now edit in WordPress editor, switch to HTML view to add the source, and DO NOT SWITCH BACK TO VISUAL VIEW. From here on, you should hang out exclusively in HTML view.
  • Change tabs to spaces before pasting the code (use VS Edit, Advanced, Untabify Selected Lines” to help here
  • Further info on the other attributes and settings you can use on the sourcecode element: http://en.support.wordpress.com/code/posting-source-code/
  • If you want to put code inline with a paragraph, use the html <code> code here </code> tags.


If the link above ever dies, here are the supported languages:

actionscript3
bash
clojure
coldfusion
cpp
csharp
css
delphi
erlang
fsharp
diff
groovy
html
javascript
java
javafx
matlab (keywords only)
objc
perl
php
text
powershell
python
r
ruby
scala
sql
vb
xml

Examples

some c# code

int x = 1;
int y = 2;
int x = 1;
int y = 2;

some c# with tabs

static SlideshowControl()
{
	DefaultStyleKeyProperty.OverrideMetadata(typeof(SlideshowControl), 
        new FrameworkPropertyMetadata(typeof(SlideshowControl)));

	Start = new RoutedCommand("Start", typeof(SlideshowControl));
	Stop = new RoutedCommand("Stop", typeof(SlideshowControl));
}

here is some sample text in Courier:

new RoutedCommand
ContentControl.RenderTransform

here is some xml

<root>
  <name first="dan" last="hanan" />
</root>

here is some xaml

<Grid x:Name="LayoutRoot"
        Background="Black">

    <ContentControl x:Name="ShellContainer"
                    RenderTransformOrigin="0.5,0.5">
      <ContentControl.RenderTransform>
        <RotateTransform x:Name="ShellContainerTransform" />
      </ContentControl.RenderTransform>

      <!-- content (Shell) injected here at runtime -->

    </ContentControl>

    <Rectangle x:Name="RotateRect"
               RenderTransformOrigin="0.5,0.5">
      <Rectangle.RenderTransform>
        <RotateTransform x:Name="RotateRectTransform" />
      </Rectangle.RenderTransform>
    </Rectangle>
  </Grid>

another one – this one from the Visual editor


<Grid x:Name="LayoutRoot"
 Background="Black">

 <ContentControl x:Name="ShellContainer"
 RenderTransformOrigin="0.5,0.5">
 <ContentControl.RenderTransform>
 <RotateTransform x:Name="ShellContainerTransform" />
 </ContentControl.RenderTransform>

 <!-- content (Shell) injected here at runtime -->

 </ContentControl>

 <Rectangle x:Name="RotateRect"
 RenderTransformOrigin="0.5,0.5">
 <Rectangle.RenderTransform>
 <RotateTransform x:Name="RotateRectTransform" />
 </Rectangle.RenderTransform>
 </Rectangle>
 </Grid>

<some stuff>
<here is some more lines for Matthew>
<dan>
<danny>
</danny>
</dan>
</here is some more lines for Matthew>
</some stuff>


some REALLY LONG XAML here

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <Image x:Name="ThumbnailImage"
        Source="{TemplateBinding ImageUrl, Converter={x:Static Converters:UriToCachedImageConverter.Default}}"
        Stretch="Fill"
        Grid.RowSpan="2">
        <Image.Style>
            <Style TargetType="{x:Type Image}">
                <Setter Property="Visibility"
                        Value="Visible" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Visibility, ElementName=PART_Player}"
                                Value="Visible">
                        <Setter Property="Visibility"
                                Value="Hidden" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>

    <MediaElement x:Name="PART_Player"
                Visibility="Hidden"
                LoadedBehavior="Manual"
                ScrubbingEnabled="True"
                Volume="{TemplateBinding Volume}"
                Grid.RowSpan="2" />

    <Grid x:Name="playbackPanel"
        Grid.Row="1"
        Background="#66000000">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <s:SurfaceButton x:Name="PART_PlayButton"
                        Grid.Column="0"
                        Style="{DynamicResource PlaybackButtonStyle}"
                        Command="{x:Static CommonControls:MediaControlBase.Play}">
            <Path Fill="{TemplateBinding Foreground}"
                Data="M0,0 L1,1 0,2"
                Stretch="Fill"
                Height="20"
                Width="15"
                Margin="0,0,0,-2" />
        </s:SurfaceButton>
        <s:SurfaceButton x:Name="PART_PauseButton"
                        Grid.Column="1"
                        Style="{DynamicResource PlaybackButtonStyle}"
                        Command="{x:Static CommonControls:MediaControlBase.Pause}">
            <StackPanel Orientation="Horizontal">
                <Path Fill="{TemplateBinding Foreground}"
                    Data="M0,0 L0,1 1,1 1,0"
                    Stretch="Fill"
                    Height="20"
                    Width="5"
                    Margin="-1,0,3,-2" />
                <Path Fill="{TemplateBinding Foreground}"
                    Data="M0,0 L0,1 1,1 1,0"
                    Stretch="Fill"
                    Height="20"
                    Width="5"
                    Margin="0,0,0,-2" />
            </StackPanel>
        </s:SurfaceButton>
        <s:SurfaceButton x:Name="PART_StopButton"
                        Grid.Column="2"
                        Style="{DynamicResource PlaybackButtonStyle}"
                        Command="{x:Static CommonControls:MediaControlBase.Stop}">
            <Path Fill="{TemplateBinding Foreground}"
                Data="M0,0 L0,1 1,1 1,0"
                Stretch="Fill"
                Height="15"
                Width="15"
                Margin="-2,0,0,-2" />
        </s:SurfaceButton>

        <s:SurfaceSlider x:Name="PART_ProgressSlider"
                        Grid.Column="3"
                        VerticalAlignment="Center"
                        Margin="45,0,40,0"
                        Orientation="Horizontal"
                        Minimum="0"
                        MoveToPointDuration="200"
                        IsMoveToPointEnabled="True"
                        IsEnabled="{TemplateBinding IsMediaOpened}"
                        Maximum="{TemplateBinding MediaEndTime}"
                        Value="{Binding MediaCurrentTime, RelativeSource={RelativeSource TemplatedParent}}" />

        <s:SurfaceToggleButton x:Name="ShowHideVolumeButton"
                            Grid.Column="4"
                            Style="{DynamicResource PlaybackToggleButtonStyle}">
            <Rectangle Fill="{DynamicResource VolumeIconBrush}"
                    Height="22"
                    Width="22"
                    Margin="0,0,0,-1" />
        </s:SurfaceToggleButton>

        <Grid x:Name="VolumeSliderPanel"
            Grid.Column="4"
            Height="200"
            Width="40"
            Margin="0,-250,0,0">
            <Grid.Style>
                <Style TargetType="{x:Type Grid}">
                    <Setter Property="Visibility"
                            Value="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=ShowHideVolumeButton}"
                                    Value="True">
                            <DataTrigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                                        Duration="0:0:0.2"
                                                        From="0"
                                                        To="1" />
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0:0:0"
                                                                    Value="{x:Static Visibility.Visible}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.EnterActions>
                            <DataTrigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                                        Duration="0:0:0.2"
                                                        From="1"
                                                        To="0" />
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                                            <DiscreteObjectKeyFrame KeyTime="0:0:0"
                                                                    Value="{x:Static Visibility.Visible}" />
                                            <DiscreteObjectKeyFrame KeyTime="0:0:0.2"
                                                                    Value="{x:Static Visibility.Collapsed}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </DataTrigger.ExitActions>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Grid.Style>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Border Background="#7F000000"
                    CornerRadius="10"
                    BorderBrush="#7FFFFFFF"
                    BorderThickness="1"
                    Grid.RowSpan="2" />
            <s:SurfaceSlider x:Name="PART_VolumeSlider"
                            Grid.Row="0"
                            Orientation="Vertical"
                            HorizontalAlignment="Center"
                            Margin="0,8"
                            Maximum="1"
                            LargeChange="0.5"
                            Value="{TemplateBinding Volume}" />
        </Grid>
    </Grid>

    <Border x:Name="ErrorMessage"
            Grid.RowSpan="2"
            Background="#C000"
            Visibility="Collapsed">
        <TextBlock Foreground="Red"
                VerticalAlignment="Center"
                TextAlignment="Center"
                TextWrapping="Wrap"
                Text="There was an error loading the video for playback. Please make sure that the file exists and is in the correct format." />
    </Border>
</Grid>

Here is some command prompt code

    dir c:\temp
    dir c:\temp

Here is some PowerShell

    Add-AppxPackage {PathToAppx}

Leave a Reply

Your email address will not be published. Required fields are marked *