Skip to content

Updated Style Guide with article about formatting code samples #1954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ The following topics explain how to contribute to the PowerShell documentation.
1. [Get started](./contributing/GETSTARTED.md)
2. [Writing PowerShell documentation](./contributing/WRITING.md)
3. [Style Guide](./contributing/STYLE.md)
4. [Formatting code blocks](./contributing/FORMATTING-CODE.md).
171 changes: 171 additions & 0 deletions contributing/FORAMTTING-CODE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
# Formatting code blocks

The existing documentation has used multiple styles, over time, and the formatting rules have changed multiple times.
We want to adopt a consistent style for PowerShell code blocks and output in our documentation.
The current Open Publishing System (OPS) we use has a syntax highlighting feature that supports many languages.
OPS also adds a **Copy** button that copies the contents of the code block to the clipboard.
This allows you to quickly paste the code into a script for testing the code example.
However, not all examples in our documentation are intended to be run.
Some code blocks are simple illustrations of a PowerShell concept.

We are defining two scenarios for code blocks:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love the delineation here, hits the nail on the head.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


1. Illustrative examples
2. Executable examples

## Formatting illustrative examples

Illustrative examples are used to explain a PowerShell concept.
They are not meant to be copied to the clipboard for execution.
These are most commonly used for simple examples that are easy to type.
They are also used for syntax examples where you are illustrating the syntax of a command.

Illustrative examples use a "naked" code fence to mark the beginning and end of the code block.
The code block may contain example output from the command being illustrated.

### Syntax block

Here is an example of a syntax block:

```
Get-Command [-Verb <String[]>] [-Noun <String[]>] [-Module <String[]>]
[-FullyQualifiedModule <ModuleSpecification[]>] [-TotalCount <Int32>] [-Syntax] [-ShowCommandInfo]
[[-ArgumentList] <Object[]>] [-All] [-ListImported] [-ParameterName <String[]>]
[-ParameterType <PSTypeName[]>] [<CommonParameters>]
```

This example illustrates all of the possible parameters of the `Get-Command` cmdlet.

Here is another syntax example describing the `for` statement in generalized terms:

```
for (<init>; <condition>; <repeat>)
{<statement list>}
```

### Simple illustration example

Here is an example illustrating PowerShell comparison operators:

```
PS> 2 -eq 2
True

PS> 2 -eq 3
False

PS> 1,2,3 -eq 2
2

PS> "abc" -eq "abc"
True

PS> "abc" -eq "abc", "def"
False

PS> "abc", "def" -eq "abc"
abc
```

Note that this example has the simplified PowerShell prompt and shows the resulting output.
In this case, we don't intend the reader to copy this example and try to run the copied code.

## Formatting executable examples

More complex examples or examples that would be useful to copy and execute should use <code>\`\`\`powershell</code> &hellip; <code>\`\`\`</code> code fence markers.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just an FYI, but I believe you can do this like:

`` ```powershell `` ... `` ``` ``

which will render as:

```powershell ... ```

Source: https://gist.github.com/mcandre/8d76e076b2495fd8b36f439ec5033116

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This formatting was submitted by an external contributor and I accepted it then. The use of spaces gets dicey (especially if you get it wrong) and is harder to see the intent of the formatting.

Output emitted by PowerShell commands should be enclosed in a **Output** code block to prevent syntax highlighting.

For example:

```powershell
Get-Command -Module Microsoft.PowerShell.Security
```

```Output
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet ConvertFrom-SecureString 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet ConvertTo-SecureString 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-Acl 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-AuthenticodeSignature 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-CmsMessage 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-Credential 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-PfxCertificate 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet New-FileCatalog 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Protect-CmsMessage 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Set-Acl 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Set-AuthenticodeSignature 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Set-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Test-FileCatalog 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Unprotect-CmsMessage 3.0.0.0 Microsoft.PowerShell.Security
```

The **Output** code label is not an official "language" supported by the syntax highlighting system.
However, this label is useful because OPS adds the "Output" label to the code box on the web page.
And this "Output" code box has no syntax highlighting.

## Understanding the COPY button

OPS also adds a **Copy** button that copies the contents of the code block to the clipboard.
This allows you to quickly paste the code into a script for testing the code example.
The **Copy** button also removes PowerShell prompts from the text before copying it to the clipboard.
The **Copy** button uses the following javascript code to remove the prompts.

```javascript
if (language === 'powershell') {
text = text.replace(/\bPS [a-z]:\\>\s?/gi, '');
}
```

Note that the regex used only matches simple prompts like "`PS C:\>`".
Prompts that include subdirectories or paths from other PSDrive providers will not be removed.
See the general guidelines, in the next section, for the appropriate use of prompts in examples.

## General guidelines

- Code fences

Markdown allows for indentation to signify a code block, but this pattern can be problematic and should be avoided.
All code blocks should be contained in a code fence.
A code fence is a block of code surrounded by <code>\`\`\`</code> strings.
The code fence markers must be on their own line before and after the code sample.
The marker at the start of the code block may have an optional language label.
See the previous discussion about illustrative and executable examples for the proper use of language labels.

- Line continuation in code samples

Avoid using line continuation characters (\`) in PowerShell code examples.
These are hard to see and can cause problems if there are extra spaces on the end of the line.
Use PowerShell splatting to reduce line length for cmdlets that have a lot of parameters.
Take advantage of PowerShell's natural line break opportunities, like after pipe (\|) characters and opening braces, parentheses, and brackets.

- PowerShell prompts in examples

PowerShell prompts should only be used in illustrative examples.
Prompts should **NOT** be used in executable examples.
For most of these examples, the prompt string should be "`PS>`".
This prompt is independent of OS-specific indicators.
Use of the prompt string is discouraged and should be limited to scenarios that are meant to illustrate command line usage.
More complex prompt strings are required for examples that illustrate commands that alter the prompt
or when the path displayed is significant to the scenario being illustrated.
The following example illustrate how the prompt changes when using the Registry provider.

```
PS C:\> cd HKCU:\System\
PS HKEY_CURRENT_USER\System\> dir


Hive: HKEY_CURRENT_USER\System


Name Property
---- --------
CurrentControlSet
GameConfigStore GameDVR_Enabled : 1
GameDVR_FSEBehaviorMode : 2
Win32_AutoGameModeDefaultProfile : {2, 0, 1, 0...}
Win32_GameModeRelatedProcesses : {1, 0, 1, 0...}
GameDVR_HonorUserFSEBehaviorMode : 0
GameDVR_DXGIHonorFSEWindowsCompatible : 0
```
43 changes: 5 additions & 38 deletions contributing/STYLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ There are several variants of PowerShell.
This table defines some of the different terms used to discuss PowerShell.

| Terminology | Definition |
| ----- | -----|
| ----- | ----- |
| PowerShell | This is the default. We are shipping PowerShell. The term PowerShell can be legitimately used to indicate any of the particular editions. This can be used to refer to the language, framework and default cmdlets, etc. |
| PowerShell Core (PSCore) | PowerShell built on .NET Core Common Language Runtime (CoreCLR) for any of the platforms. |
| Windows PowerShell | PowerShell built on .NET Common Language Runtime (CLR). Windows PowerShell ships only on Windows and requires the complete CLR. |
Expand Down Expand Up @@ -84,43 +84,6 @@ Adding or removing H2 causes a build break.

This [`Write-Host`](..\reference\6\Microsoft.PowerShell.Utility\Write-Host.md) cmdlet uses the **-Object** parameter to ...

## Formatting code blocks

* All PowerShell syntax blocks should use <code>\`\`\`powershell</code> &hellip; <code>\`\`\`</code> code fence markers.

* Do **NOT** start PowerShell commands with the PowerShell prompt ("`PS C:\>`").

* Avoid using line continuation characters (\`) in PowerShell code examples.
These are a hard to see and can cause problems if there are extra spaces on the end of the line.

* Output emitted by PowerShell commands should be enclosed in a naked code block to prevent it from recieving syntax highlighting.

For example:

```powershell
Get-Command -Module Microsoft.PowerShell.Security
```

```
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet ConvertFrom-SecureString 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet ConvertTo-SecureString 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-Acl 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-AuthenticodeSignature 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-CmsMessage 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-Credential 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Get-PfxCertificate 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet New-FileCatalog 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Protect-CmsMessage 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Set-Acl 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Set-AuthenticodeSignature 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Set-ExecutionPolicy 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Test-FileCatalog 3.0.0.0 Microsoft.PowerShell.Security
Cmdlet Unprotect-CmsMessage 3.0.0.0 Microsoft.PowerShell.Security
```

## Lists

* Do not end list items with a period (unless they contain multiple sentences)
Expand Down Expand Up @@ -162,6 +125,10 @@ There are special rules for linking to reference topics from conceptual topics.

\[TO DO\] - document special rules

## Next steps

See [Formatting code blocks](FORMATTING-CODE.md).

<!-- External URLs -->
[pascal-case]: https://en.wikipedia.org/wiki/PascalCase
[issue1806]: https://github.com/PowerShell/PowerShell-Docs/issues/1806
Expand Down