# Discourse Guide: Code Formatting

**URL:** https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587
**Category:** Meta
**Created:** [June 8, 2017, 3:59am UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587 "2017-06-08T03:59:05Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jomaxro](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/jomaxro/32/47935_2.png) [@jomaxro](https://discourse.stonehearth.net/u/jomaxro)
#### Post date: [June 8, 2017, 3:59am UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/1 "2017-06-08T03:59:05Z")

</div>

# Posting code or preformatted text on the forums

Have a line of code you want to share on the forums? Perhaps you need to share an error log or file in your mod? Reading code without formatting can be tough, if not impossible if the code depends on spacing (looking at you, Python). This guide focuses on Markdown, but the forums also support a subset of BBCode and HTML, so pick whichever style you like best.

## Inline code formatting

To format a specific section of text, use a single back-tick on each side of the code. This can be done within regular text, or when the code is on its own line. Inline formatting does not apply any automatic code formatting (see below), and only applies the “code style” to the text between back-ticks.

### Example 1:

**Typing:**

`This is a line of code`

**Produces:**

`This is a line of code`

### Example 2:

**Typing:**

Here’s some text with `a bit of code` inside!

**Produces:**

Here’s some text with `a bit of code` inside!

## Entire line code formatting

To format an entire line, add 4 spaces at the start of a line. All text until the next line break will be code.

### Example:

**Typing:**

```
All text after 4 spaces will be formatted.
```

**Produces:**

```
All text after 4 spaces will be formatted.

```

## Block code formatting

While you can use the 4-space method to produce multiple lines of code, this can be tedious, especially if you are copying and pasting and need to type 4 spaces on multiple lines of text.

Instead, use 3 backticks (```) on their own line above and below your code. Be sure there are no spaces on the lines with the backticks, or this will not work.

### Example:

**Typing:**

```
```
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
```
```

**Produces:**

```auto
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}

```

## Automatic code styling

The forums will _try_ to automatically detect what language you are typing and style your code accordingly. This might be nice for sharing code, but tends to produce strange results when sharing other preformatted text. To avoid the automatic styling, declare your code to be “raw text” by typing `text` after the first 3 back ticks.

### Example:

**Typing:**

```
```text
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}
```
```

**Produces:**

```nohighlight
public class CodeFormatting {
    public static void main(String[] args) {
        System.out.println("I can format code now!");
    }
}

```

In addition to `text`, you can manually choose the formatting of a code block with the following selectors: `apache`, `bash`, `coffeescript`, `cpp`, `cs`, `css`, `diff`, `handlebars`, `http`, `ini`, `java`, `javascript`, `json`, `makefile`, `markdown`, `nginx`, `objectivec`, `perl`, `php`, `python`, `ruby`, `sql`, `xml`.

---

<div class="post-metadata">

### Author: ![coasterspaul](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/coasterspaul/32/47876_2.png) [@coasterspaul](https://discourse.stonehearth.net/u/coasterspaul)
#### Post date: [June 8, 2017, 1:44pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/2 "2017-06-08T13:44:49Z")

</div>

> [@jomaxro](#):
>
> you can manually choose the formatting of a code block with the following selectors

Someone needs to get the Discourse coders to add Lua for us 😄

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/jomaxro/32/47935_2.png) [@jomaxro](https://discourse.stonehearth.net/u/jomaxro)
#### Post date: [June 8, 2017, 4:37pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/3 "2017-06-08T16:37:11Z")

</div>

> [@coasterspaul](#):
>
> Someone needs to get the Discourse coders to add Lua for us 😄

It should just be a site setting (no need to involve coders). Perhaps @Relyss could add `lua` to the list in site settings. (`highlighted languages` setting).

---

<div class="post-metadata">

### Author: ![Relyss](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/relyss/32/41785_2.png) [@Relyss](https://discourse.stonehearth.net/u/Relyss)
#### Post date: [June 8, 2017, 4:47pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/4 "2017-06-08T16:47:23Z")

</div>

It says including too many languages can impact performance. But I could always remove languages that we don’t use often here too 🤔

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/jomaxro/32/47935_2.png) [@jomaxro](https://discourse.stonehearth.net/u/jomaxro)
#### Post date: [June 8, 2017, 4:48pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/5 "2017-06-08T16:48:14Z")

</div>

Looking at the list, I don’t know any reason there would be `objectivec`, `coffeescript`, or `apache` code shared. Also, on another site I admin we’ve added 3 languages and haven’t seen any performance difference. I’d like to assume that Discourse allows you to add at least a few non-default languages without tanking performance.

---

<div class="post-metadata">

### Author: ![Relyss](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/relyss/32/41785_2.png) [@Relyss](https://discourse.stonehearth.net/u/Relyss)
#### Post date: [June 8, 2017, 4:52pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/6 "2017-06-08T16:52:26Z")

</div>

Testing…

```lua
function Shop:get_name()
   return self._sv.name
end

```

It works! :jubilant:

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/jomaxro/32/47935_2.png) [@jomaxro](https://discourse.stonehearth.net/u/jomaxro)
#### Post date: [June 8, 2017, 4:54pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/7 "2017-06-08T16:54:07Z")

</div>

~~Does it? There’s not formatting on that, it’s just raw text.~~  
It works!

---

<div class="post-metadata">

### Author: ![Relyss](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/relyss/32/41785_2.png) [@Relyss](https://discourse.stonehearth.net/u/Relyss)
#### Post date: [June 8, 2017, 5:00pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/8 "2017-06-08T17:00:23Z")

</div>

I had to refresh the browser to see it, was showing as plain text first.

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea2.discourse-cdn.com/flex022/user_avatar/discourse.stonehearth.net/jomaxro/32/47935_2.png) [@jomaxro](https://discourse.stonehearth.net/u/jomaxro)
#### Post date: [June 8, 2017, 5:01pm UTC](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587/9 "2017-06-08T17:01:00Z")

</div>

> [@Relyss](#):
>
> I had to refresh the browser to see it

Thought I did that…sorry!
