Friday, 24 October 2014

Writing A Writing Tool - Part Two

Wow - Part One got a few visitors! Thanks for popping by - now on with Part Two.

Seth Ladd from the Dart team dropped a comment on the code - you'll notice all fields are lower case now :-) This was an easy change thanks to Dart Editor's built in refactoring. I have added more fields to the Chapter class and we can now create a chapter based on just the title. Named constructors coming in handy here. Two methods on Novel allow adding of Chapters and they return a reference for adding more details.

class Novel {

  String title;
  String author;
  List chapters;
  Chapter recent;

  Novel(this.title, {
    chapters = new List();

  Chapter addPlaceholder(Title) {
    var c = new Chapter.minimal(title);
    recent = c;
    return c;

  Chapter addChapter(Title, Summary, Characters) {
    var c = new Chapter.detailed(Title, Summary, Characters);
    recent = c;
    return c;

class Chapter {
  String title = "TODO";
  String summary = "TODO";
  String characters = "TODO";
  String pacing = "TODO";
  String atmosphere = "TODO";
  String length = "TODO";
  String locations = "TODO";

  Chapter.detailed(this.title, this.summary, this.characters);

Of course, our presentation layer needs a little update for all these new fields:

void txtOut(Novel n) {

  print("${n.chapters.length} Chapters");
  n.chapters.forEach((c) {
    print("~" * 80);
    print("- " * 40);
    print("CHARACTERS : " + c.characters);
    print("LOCATIONS  : " + c.locations);
    print("PACING     : " + c.pacing);
    print("ATMOSPHERE : " + c.atmosphere);
    print("LENGTH     : " + c.length);

The test novel needs expand to use the new methods and fields.

void main() {

  print("Novel Planner v0.2");


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

  cur = n.addChapter("On A Boat", "Sailing the sea and meeting the crew.", "Sam the Sailor");
  cur.pacing = "Lazy";
  cur.atmosphere = "Inviting";
  cur.length = "short";
  cur.locations = "The North Sea";

  cur = n.addChapter("On A Plane", "Flying High and introduce ticking bomb plot.", "Petra the Pilot");
  cur.pacing = "Rapid";
  cur.atmosphere = "Tense";
  cur.length = "medium";

  cur = n.addChapter("The Moonbase", "Introduces the new location and Rab the Robot.", "Rab the Robot");
  cur.pacing = "Slow";
  cur.atmosphere = "Sterile";
  cur.length = "long";
  cur.locations = "Earth's Moon";

  cur = n.addPlaceholder("Epilogue");


Grab the code from Github -

Right, I think that is looking good now. I am going to feed in my current novel plan. Part Three will be based on tweaks from using it for real and suggestions from comments. Dart has been a very productive language to write this in - glad I chose it over Python for this task. Happy coding and novel writing!