GithubHelp home page GithubHelp logo

jonnygreenwald / fountain-js Goto Github PK

View Code? Open in Web Editor NEW
27.0 6.0 8.0 2.09 MB

A simple parser for Fountain, a markup language for formatting screenplays.

License: MIT License

TypeScript 99.89% JavaScript 0.11%
filmmaking javascript markup-language screenplays screenwriting typescript

fountain-js's Introduction

Fountain-js

NPM

A simple parser for Fountain, a markup language for formatting screenplays. Originally based on Matt Daly's Fountain.js.

Special thanks to Nathan Hoad for the Fountain-js package namespace.

Supports up to v 1.1 of the Fountain syntax.

Install

npm install fountain-js

How To

Import Fountain and create a new instance of it.

import { Fountain } from 'fountain-js';

let fountain = new Fountain();

An extended example.

import { Fountain } from 'fountain-js';
import * as assert from 'assert';

const text = `.OPENING TITLES

            > BRICK & STEEL <
            > FULL RETIRED <

            SMASH CUT TO:`;
        
let fountain = new Fountain();

let output = fountain.parse(text);
let actual = output.html.script;

const expected = '<h3>OPENING TITLES</h3><p class="centered">BRICK & STEEL <br /> FULL RETIRED</p><h2>SMASH CUT TO:</h2>';

assert.strictEqual(actual, expected);

Output

The output provided by Fountain-js is an object literal in the format { title: '...', html: { title_page: '...', script: '...' } }.

Fountain-js is natively written in Typescript, therefore a Script interface is also available:

interface Script {
    title: string;
    html: {
        title_page: string,
        script: string
    };
    tokens: Token[];
}

Parser Tokens

If you want access to the tokens that Fountain-js generates, simply attach a true parameter to your parse calls. Requesting tokens adds a tokens property to the Script object generated by Fountain-js.

let output = fountain.parse(script, true);
console.log(output.tokens);

Alternatively, you can also access the tokens directly from the tokens property of the class after parsing as well.

let output = fountain.parse(script);
console.log(fountain.tokens);

Token Structure

Version 1.0 of Fountain-js creates a flat token strucure and is not like a usual AST.

Below is a small sample of the tokens output from Brick & Steel:

[ 
  ..., 
  { type: "scene_heading", text: "EXT. BRICK'S PATIO - DAY", scene_number: "1"},
  { type: "action", text: "A gorgeous day. The su...emplating -- something."},
  { type: "action", text: "The SCREEN DOOR slides ...es with two cold beers."},
  { type: "dialogue_begin"},
  { type: "character", text: "STEEL"},
  { type: "dialogue", text: "Beer's ready!"},
  { type: "dialogue_end"},
  { type: "dialogue_begin"},
  { type: "character", text: "BRICK"},
  { type: "dialogue", text: "Are they cold?"},
  { type: "dialogue_end"},
  { type: "page_break"},
  { type: "dialogue_begin"},
  { type: "character", text: "STEEL"},
  { type: "dialogue", text: "Does a bear crap in the woods?"},
  { type: "dialogue_end"},
  { type: "action", text: "Steel sits. They laugh at the dumb joke."},
  { type: "dialogue_begin"},
  { type: "character", text: "STEEL"},
  { type: "parenthetical", text: "(beer raised)"},
  { type: "dialogue", text: "To retirement."},
  { type: "dialogue_end"},
  { type: "dialogue_begin"},
  { type: "character", text: "BRICK"},
  { type: "dialogue", text: "To retirement."},
  { type: "dialogue_end"}
  ...
]

As shown above, Fountain-js attaches some extra tokens, such as 'dialogue_begin' and 'dialogue_end' to block together dialogue sections.

In the case of dual dialogue, this allows Fountain-js to attach a dual property to blocks of dialogue at the dialogue_begin token to indicate page positioning.

[
  ...
  { type: 'action', text: 'The men look at each other.' },
  { type: 'dual_dialogue_begin'},
  { type: 'dialogue_begin', dual: 'left'},
  { type: 'character', text: 'STEEL' },
  { type: 'dialogue', text: 'Screw retirement.' },
  { type: 'dialogue_end'},
  { type: 'dialogue_begin', dual: 'right'},
  { type: 'character', text: 'BRICK' },
  { type: 'dialogue', text: 'Screw retirement.' },
  { type: 'dialogue_end'},
  { type: 'dual_dialogue_end'},
  { type: 'transition', text: 'SMASH CUT TO:' }
  ...
]

Additionally, Fountain-js provides a Typescript interface for a Token as follows:

interface Token {
    type: string;
    is_title?: boolean;
    text?: string;
    scene_number?: string;
    dual?: string;
    depth?: number;

    addTo(tokens: Token[]): Token[];
}

Inline Lexing

Originally, the text within tokens would have their inline markup, e.g. emphasis, underline, or line breaks, converted to HTML; however, given some use cases, this is not practical for all library users.

As of version 1.1.2, any inline markup will remain unchanged in the token text.

[
  ...
  { type: 'action', text: 'Screaming, chaos, *frenzy*.\nThree words that apply to this scene.' },
  ...
]

This means you'll have to perform an inline lex on the token text when processing your tokens.

As a suggestion, one can import and extend the InlineLexer class. Its inline property and reconstruct method can be overwritten and modified to suit your needs if something other than HTML output is desired.

Fountain.ts

For those looking for Fountain.ts, please note that this package has been deprecated in its original form and is now Fountain-js. Please source and upgrade packages from the Fountain-js NPM package to receive all updates and fixes.

fountain-js's People

Contributors

dependabot[bot] avatar jonnygreenwald avatar zenibako avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

fountain-js's Issues

Doesn't seem to recognise `!` and `@`

! can be used to symbolise a definite action.
@ can be used to symbolise a definite character name for the dialogue.

It seems to group @ as Action instead.
Maybe the original library was made before the ! and @ were added to the specs.

import { Fountain } from 'fountain' doesn't work

I ran npm install fountain
and then import { Fountain } from 'fountain'

[plugin vite:dep-scan] Failed to resolve entry for package "fountain". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "fountain". The package may have incorrect main/module/exports specified in its package.json.

    node_modules/vite/dist/node/chunks/dep-9c153816.js:29537:10:
      29537 │     throw new Error(`Failed to resolve entry for package "${id}". ` +
            ╵           ^

    at packageEntryFailure (/Users/pj/software/petersir/sveltekit/node_modules/vite/dist/node/chunks/dep-9c153816.js:29537:11)
    at resolvePackageEntry (/Users/pj/software/petersir/sveltekit/node_modules/vite/dist/node/chunks/dep-9c153816.js:29533:9)
    at tryNodeResolve (/Users/pj/software/petersir/sveltekit/node_modules/vite/dist/node/chunks/dep-9c153816.js:29349:20)
    at Context.resolveId (/Users/pj/software/petersir/sveltekit/node_modules/vite/dist/node/chunks/dep-9c153816.js:29166:28)
    at Object.resolveId (/Users/pj/software/petersir/sveltekit/node_modules/vite/dist/node/chunks/dep-9c153816.js:38271:55)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async resolve (/Users/pj/software/petersir/sveltekit/node_modules/vite/dist/node/chunks/dep-9c153816.js:38474:26)
    at async /Users/pj/software/petersir/sveltekit/node_modules/vite/dist/node/chunks/dep-9c153816.js:38612:34
    at async callback (/Users/pj/software/petersir/sveltekit/node_modules/esbuild/lib/main.js:917:28)
    at async handleRequest (/Users/pj/software/petersir/sveltekit/node_modules/esbuild/lib/main.js:697:30)

  This error came from the "onResolve" callback registered here:

Character names with hashes (#) are parsed incorrectly

First, thanks so much maintaining this project. I works very well, and has saved me a ton of time.

I think I found a bug related to CHARACTER NAMES with hashes (#). Specifically, this:

DISGRUNTLED CITIZEN #⁠1
(standing)
Excuse me. How much did it cost?

Gets tokenized like this:

{
    "type": "action",
    "text": "DISGRUNTLED CITIZEN #1<br />(standing)<br />Excuse me. How much did it cost?"
}

(I've verified with a few different instances.)

Is this an invalid Fountain character name, or a bug? Thanks!

Parentheticals can't be italicized

If a parenthetical is italicized, it won't match the parenthetical regular expression. I've seen scripts where writers will italicize the odd parenthetical. If you want to add support, here's an updated regular expression for token.ts.

const parts: string[] = match[3].split(/(\*?\(.+\)\*?)(?:\n+)/).reverse();

Thanks!

Question: Nesting dialog

Is there any reason why dialog_begin and dialog_end couldn't be

{
  type: "dialog",
  lines: [
    { character: "Jen", line: "Nice to meet you" },
    ...
  ]
}

Seems like it'd make parsing a bit easier. I'm using this in React, and needing a way to replace string matches with react components and finding these tokens a little hard to work with.

If it's helpful or wanted I can help code

Action with a newline gets a <br /> inserted

If you have a line of action with a newline (\n), it gets replaced with a <br />.

The problem seems to come from passing this.tokens into to_html(). If, instead, you make a deep copy (so that to_html() doesn't modify the instance), it works as expected. For example:

      const tokenCopy = Utils.deepCopy(this.tokens) as Token[];
      return {
        title: titleString,
        html: {
          title_page: tokenCopy
            .filter(token => token.is_title)
            .map(token => this.to_html(token))
            .join(''),
          script: tokenCopy
            .filter(token => !token.is_title)
            .map(token => this.to_html(token))
            .join(''),
        },
        tokens: getTokens ? this.tokens : undefined,
      };

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.