Task 386: .MD File Format

Task 386: .MD File Format

The .MD file format is the Markdown file format, a lightweight markup language for creating formatted text using plain text. The specifications can be found in the CommonMark specification.

  1. List of all the properties of this file format intrinsic to its file system:
  • Filename extensions: .md, .markdown
  • Internet media type: text/markdown
  • Uniform Type Identifier (UTI): net.daringfireball.markdown
  • UTI Conformation: public.plain-text
  • Magic Number: None
  • Type of format: Open file format

Two direct download links for files of format .MD:

http://sample-file.bazadanni.com/download/txt/markdown/sample.md

https://gist.githubusercontent.com/rt2zz/e0a1d6ab2682d2c47746950b84c0b6ee/raw/markdown-sample.md

Ghost blog embedded HTML JavaScript for drag and drop:

Drag and drop a .MD file here
  1. Python class:
class MDFile:
    def __init__(self, filename):
        self.filename = filename
        self.content = None

    def open_and_read(self):
        with open(self.filename, 'r', encoding='utf-8') as f:  # Decode as UTF-8
            self.content = f.read()

    def get_content(self):
        return self.content

    def write(self, content):
        with open(self.filename, 'w', encoding='utf-8') as f:
            f.write(content)

    def print_properties(self):
        print("Filename extensions: .md, .markdown")
        print("Internet media type: text/markdown")
        print("Uniform Type Identifier (UTI): net.daringfireball.markdown")
        print("UTI Conformation: public.plain-text")
        print("Magic Number: None")
        print("Type of format: Open file format")
  1. Java class:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MDFile {
    private String filename;
    private String content;

    public MDFile(String filename) {
        this.filename = filename;
        this.content = null;
    }

    public void openAndRead() throws IOException {
        StringBuilder sb = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
            String line;
            while (line = br.readLine() != null) {
                sb.append(line).append("\n");
            }
        }
        content = sb.toString();
    }

    public String getContent() {
        return content;
    }

    public void write(String content) throws IOException {
        try (FileWriter fw = new FileWriter(filename)) {
            fw.write(content);
        }
    }

    public void printProperties() {
        System.out.println("Filename extensions: .md, .markdown");
        System.out.println("Internet media type: text/markdown");
        System.out.println("Uniform Type Identifier (UTI): net.daringfireball.markdown");
        System.out.println("UTI Conformation: public.plain-text");
        System.out.println("Magic Number: None");
        System.out.println("Type of format: Open file format");
    }
}
  1. JavaScript class (for Node.js):
const fs = require('fs');

class MDFile {
  constructor(filename) {
    this.filename = filename;
    this.content = null;
  }

  openAndRead() {
    this.content = fs.readFileSync(this.filename, 'utf8');  // Decode as UTF-8
  }

  getContent() {
    return this.content;
  }

  write(content) {
    fs.writeFileSync(this.filename, content, 'utf8');
  }

  printProperties() {
    console.log("Filename extensions: .md, .markdown");
    console.log("Internet media type: text/markdown");
    console.log("Uniform Type Identifier (UTI): net.daringfireball.markdown");
    console.log("UTI Conformation: public.plain-text");
    console.log("Magic Number: None");
    console.log("Type of format: Open file format");
  }
}
  1. C class (using C++ for class support):
#include <iostream>
#include <fstream>
#include <string>

class MDFile {
private:
    std::string filename;
    std::string content;

public:
    MDFile(const std::string& fn) : filename(fn), content("") {}

    void openAndRead() {
        std::ifstream file(filename);
        if (file.is_open()) {
            std::string line;
            while (std::getline(file, line)) {
                content += line + "\n";
            }
            file.close();
        } else {
            std::cerr << "Unable to open file" << std::endl;
        }
    }

    std::string getContent() const {
        return content;
    }

    void write(const std::string& cnt) {
        std::ofstream file(filename);
        if (file.is_open()) {
            file << cnt;
            file.close();
        } else {
            std::cerr << "Unable to open file for writing" << std::endl;
        }
    }

    void printProperties() const {
        std::cout << "Filename extensions: .md, .markdown" << std::endl;
        std::cout << "Internet media type: text/markdown" << std::endl;
        std::cout << "Uniform Type Identifier (UTI): net.daringfireball.markdown" << std::endl;
        std::cout << "UTI Conformation: public.plain-text" << std::endl;
        std::cout << "Magic Number: None" << std::endl;
        std::cout << "Type of format: Open file format" << std::endl;
    }
};