Thursday, 23 October 2014

Writing A Writing Tool - Part One

This is first in a series of blog posts where I will blog the creation of a simple writing tool as a Dart command line application. The background to this is NaNoWriMo which challenges anyone in the world to write a 50,000 word novel during November. Luckily you are allowed to plan in advance (there is not last minute revealed theme or requirement). I started with pen and paper which has its benefits but is too static for my liking! In the past I have written presentation materials in Python to escape Powerpoint and don't want to live in a database or spreadsheet for storytelling.

Important : This series will probably not have any challenging code but is a real world problem for me! Sorry to bore the experts :-)

Okay to get started we will need a basic data structure.

class Novel
{
  String Title;
  String Author;
  List Chapters;

  Novel(this.Title, this.Author){
    Chapters = new List();
  }

  void addChapter(Title, Summary, Characters ){  
    Chapters.add(new Chapter(Title, Summary, Characters));
  }
}

class Chapter
{
  String Title = "TODO";
  String Summary = "TODO";
  String Characters = "TODO";

  Chapter(this.Title,
          this.Summary,
          this.Characters){}
}

We can output using print - maybe HTML later.

void txtOut(Novel n)
{
  n.Chapters.forEach( (c) {
    print("~" * 80);
    print(c.Title.toUpperCase());
    print("- " * 40);
    print(c.Summary);
    print("");
    print("CHARACTERS : " + c.Characters);
    print("");
  } );
}

Finally we can create a 2 chapter novel very easily!

void main() {

  print("Novel Planner v0.1");
  print("");
  
  //---

  Novel n = new Novel("Dart Chronicles", "D. Art");

  n.addChapter("On A Boat", "Sailing the sea and meeting the crew.", "Sam the Sailor");
  n.addChapter("On A Plane", "Flying High and introduce ticking bomb plot.", "Petra the Pilot");

  //---
  txtOut(n);
}

Our novel plan so far:

Novel Planner v0.1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ON A BOAT
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Sailing the sea and meeting the crew.

CHARACTERS : Sam the Sailor

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ON A PLANE
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Flying High and introduce ticking bomb plot.

CHARACTERS : Petra the Pilot


I did say this is very simple so I hope Dart newbies can chip in! All code will be kept on GitHub. I welcome patches.

Grab the code from Github - https://github.com/daftspaniel/dart-nanowrimoplanner

Next iteration will add more fields for a chapter and improve the output. Already I like it - I can fiddle with details and the order but still get a neat output each time.