zero's a life

An extra chance.

Syntax Highlighting for Octopress

| Comments

It’s relatively simple, but I always forget how it’s done. In the interest of documenting useful information for myself, here is how to use syntax highlighting with code blocks in Octopress.

Here are some examples of languages I typically use:

1
2
3
4
5
6
7
8
9
10
11
private IEnumerator GUIMoveToPosition(Vector3 newPosition, float duration, RectTransform rect) {
  float elapsedTime = 0;
  Vector3 startingPos = rect.position;
  while(elapsedTime < duration) {
    rect.position = Vector3.Lerp(startingPos,
                                 newPosition,
                                 elapsedTime / duration);
    elapsedTime += Time.deltaTime;
    yield return null;
  }
}
1
2
3
4
5
6
def update(self, time_elapsed):
    self.tick += 1
    self.total_time += time_elapsed

    if self.mode:
        self.mode.update(time_elapsed)
1
2
3
4
5
6
7
8
9
10
11
12
(defn valid-string? [s]
  "Valid strings are non-empty strings that do not contain
  [ (commonly used to denote parts of the song and which rappers are
  rapping) and : (commonly used in the album info and transcriber
  info)."
  (and (not (contains-char? s \[))
       (not (contains-char? s \:))
       (not (contains-char? s \"))
       (not (contains-char? s \())
       (seq s)))
;;; http://clojuredocs.org/clojure_core/clojure.core/empty_q
;;; Please use the idiom (seq x) rather than (not (empty? x))

Bonus: Help me refactor this code

I think there’s some way to refactor these two similar functions so they don’t share code, but I don’t see it. If you have a clever way to offload the common functionality, let me know in the comments (gists preferred).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// NOTE: there are some differences between the two functions
// because I'm still adding funcitonality.  I think there should be
// a way to pull out some of the common functionality so I don't
// have two functions that share code.  Maybe by separating the
// boundary checks from the actual "counting" steps?

void Countdown () {
  int iTargetHp = iHp + 1;

  // If the rolling hp reaches the hit point above iHp then we can
  // stop the rolling because each RollEvent effectively moves past
  // the current iRolHp.
  if(iRolHp == iTargetHp) {
    // Start Coroutine to return reels to original positions
    StartCoroutine(WaitAndResetReels(RollDir.Down));

    CancelInvoke("Countdown");
  }
  if(iRolHp <= 0) {
    StartCoroutine(WaitAndResetReels(RollDir.Down));

    // TRUE DEATH!
    CancelInvoke("Countdown");
  }
  // If the rolling HP is larger than the target HP and positive,
  // then roll down.
  if(iRolHp > iTargetHp || iRolHp > 0) {
    RollEvent(RollDir.Down);
  }

  iRolHp = iRolHp + (int)RollDir.Down;

}

void Countup() {
  int iTargetHp = iHp - 1;

  // If the rolling hp reaches the hit point below iHp then we can
  // stop rolling because each RollEvent effectively moves past the
  // current iRolHp.
  //
  // Also take care of some nasty edge cases resulting in two calls
  // to WaitAndResetReels.
  if(iRolHp == iTargetHp & iRolHp < iMaxHp & iRolHp != iMaxHp) {
    StartCoroutine(WaitAndResetReels(RollDir.Up));

    CancelInvoke("Countup");
  } else if(iRolHp >= iMaxHp) {
    iHp = iMaxHp;
    StartCoroutine(WaitAndResetReels(RollDir.Up));

    // Maxed out HP.
    CancelInvoke("Countup");
  }

  // If the rolling HP is smaller than the target HP and less than
  // Max HP, then roll up.
  if(iRolHp < iTargetHp || iRolHp < iMaxHp) {
    RollEvent(RollDir.Up);
  }

  iRolHp = iRolHp + (int)RollDir.Up;

}

Comments