Code formatting in CoderLegion

posted 5 min read

CoderLegion has an article editor that can format text. We will see the editor when we press the Post An Article button. In this article, we will talk about how to format text or code using the code formatter button and see how it looks like when html is finally rendered.

1.  How it is done in coderlegion editor #

From the coderlegion editor, follow the following steps.

  • Paste code
  • Select the code
  • Press the {} button in the code editor. See image 1.

Image 1

2.  Output #

1.  Python code #

"""Python module docstring
"""

class Study:
    def __init__(self, name):
        self.name = name

    def get_name(self):
        """Get name"""
        return self.name


# Declare name
name = 'Peter'

mystrlist = ['USA', 'Algeria']
mynumlist = [4, 8, 6]
myfloatlist = [25.2, 8.6, 58.5]
mydict = {'job': 'programmer', 'age': 45}
mytuple = (5, 'island')

a = Study(name)  # instantiate class

# Print name.
print(a.get_name())

2.  Javascript code #

// Modify gre value.
document.getElementById("gre").innerHTML = "World Cup";

class Car {
    constructor(name, age) {
      this.name = name;
      this.age= age;
    }
}

/*
Multi-line comment.
Define a new Car.
*/    
let myCar1 = new Car("Acura", 2);
const cars = ["toyota", "Bentley", "audi"];
let x = 100 + 50;

let mycomment = "The covid-19 variants.";
let pattern = /[^h]/g;

function get_system(param_x, param_y) {
    return param_x + param_y;
}

3.  PHP code #

variables / strings

<?php
$color = "blue";
echo "My shirt is " . $color . "<br>";
?> 

function

<?php
function welcome_message() {
  echo "Hello world!";
}

welcome_message(); // call the function
?>

class

<?php
class Building {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>

4.  C++ code #

comments / variables

#include <iostream>
using namespace std;

int main() {

    // This is a comment
    int num_experience = 10;

    cout << num_experience;

    /* The code below will print the words Hello World!
    to the screen, and it is amazing */
    cout << "Hello World!";

    return 0;
}

class

#include <string>
#include <iostream>
using namespace std;

class MyClass {
    public:
        int num;
        string my_str;
};

int main() {
    MyClass sample;  // Create an object of MyClass

    // Access attributes and set values
    sample.num = 15; 
    sample.my_str = "Some text";

    // Print attribute values
    cout << sample.num << "\n";
    cout << sample.my_str;
    return 0;
}

preprocessor

#include <iostream>
using namespace std;

#define PI 3.14159
#define MIN(a,b) (((a)<(b)) ? a : b)

#ifndef NULL
    #define NULL 0
#endif

int main () {
    cout << "Value of PI :" << PI << endl; 

    cout << MIN(2, 6) << endl;

    return 0;
}

5.  JSON format #

{
    "fruit": "Apple",
    "size": 45,
    "invoiceTime": "2018-12-10T13:49:51.141Z",
    "total": 336.36,
    "q1": {
        "question": "5 + 7 = ?",
        "options": [
            "10",
            "11",
            "12",
            "13"
        ],
        "answer": "12"
    }
}

6.  CSS format #

@import url("./border.css");
@import url("./font.css");

#box {
    width: 200px;
    height: 200px;
    background-color: rgb(14, 179, 142);
}

.center {
    text-align: center;
    color: red;
}

/* This is
a multi-line
comment */
p {
    background-color: red !important;
}

@font-face {
    font-family: 'Helvetica';
    src:  url('Helvetica') format('woff'),
          local('Helvetica.woff') format('woff');
}

/* This is a single-line comment */
a[target=_blank] {
    background-color: yellow;  /* Set text color to red */
}

7.  XML format #

<!--Tallest buildings-->
<buildings>
  <rank id="1">
    <name>Burj Khalifa</name>
    <city>Dubai</city>
  </rank>
  <rank id="2">
    <name>Merdeka 118</name>
    <city>Kuala Lumpur</city>
  </rank>
</buildings>

8.  JAVA code #

/**
* javadoc: main class.
*/
public class Main {
  public static void main(String[] args) {
    // This is a comment.
    System.out.println("Hello Mars");
    
    /* Multi-line
    comment
    */    
    System.out.println("Hello Earth"); // Earth
    
    int num_ships = 100;
    String ship_name = "Lantern";
  }
}

9.  C Sharp code #

using System;

namespace coderlegion 
{
  class Program
  { 
    // main 
    static void Main(string[] args)
    {
      /*
      multi-line
      comment
      */
      Console.WriteLine("Hello Coderlegion !"); // world
    }
  }
}

10.  Perl code #

use Config; #config
my $secure_perl_path = $Config{perlpath};

sub foo ($left, $right) {
    return $left + $right;
}

=begin
multi-line
comment
=cut
if ($^O ne 'VMS') {
    $secure_perl_path .= $Config{_exe}

    #single line comment
    unless $secure_perl_path =~ m/$Config{_exe}$/i;
}

11.  Ruby code #

function

def sayGoodnight(name)
  result = "Goodnight, " + name
  return result
end

# Time for bed...
puts sayGoodnight("John-Boy")
puts sayGoodnight("Mary-Ellen")

class

=begin
multi-line
comment
=end

class Song
  def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
  end
end

12.  SQL code #

CREATE TABLE Country (
    CountryID int,
    Name varchar(255),
    Population int
);

SELECT * FROM Orders WHERE OrderDate='2022-12-11'

13.  ini format #

; comment

[Section-name]
key=value

myoption=true # in-line comment

number1=123
sample_float=123.12

; boolean true/false
beautiful=true
dangerous=false

14.  bash code #

variable

#!/bin/bash
# variable example

sports1=football
sports2=basketball
echo $sports1 $sports2

keyword

#!/bin/bash
echo "Input a directory name"
read new_dir
`mkdir $new_dir`
If you read this far, tweet to the author to show them you care. Tweet a Thanks
This is very helpful. Wish I saw this when trying to post my first one on here.

More Posts

Tips and Tricks in article formatting for Coderlegion

James Dayal - Feb 9, 2024

Markdown Divs in CoderLegion

James Dayal - Sep 3, 2023

A Love Story in Code: Building My Self-Hosted Photo Album

kitfu10 - Mar 20

The Power of Higher Order Functions in JavaScript: Writing Cleaner and More Efficient Code

Mubaraq Yusuf - Mar 19

Efficient Approach to Calculate the Sum of Numbers Divisible by 4 in Python (Part 2: Full Code Implementation)

Ferdy - Mar 17
chevron_left