Skip to content

Commit

Permalink
Remove unnecessary code; clarify comments
Browse files Browse the repository at this point in the history
  • Loading branch information
start committed Oct 10, 2020
1 parent 67b2baf commit c47eb66
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 107 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/Implementation/Parsing/Inline/Tokenizing/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -997,16 +997,16 @@ class Tokenizer {
return true
}

// Well, we couldn't successfully close the convention, so we've got to backtrack. For now, a
// convention can only fail to close if:
// Well, we couldn't successfully close the convention, so we've got to backtrack. A convention can
// only fail to close if:
//
// 1. It must be followed by one of a set of specific conventions, and
// 2. None of those conventions could be opened
this.backTrackToBefore(open)

// We know for a fact that we won't be able to close any other conventions at our new (backtracked)
// markup index; we already tried to close all of them when we opened the now-failed convention. So
// let's just return false and let the tokenizer continue at the next step.
// markup index; we had already tried to close all of them back when we opened this now-failed
// convention. So let's just return false and let the tokenizer continue at the next step.
return false
}

Expand Down
55 changes: 20 additions & 35 deletions src/Implementation/Up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,47 @@ export class Up {

// Converts Up markup into HTML and returns the result.
parseAndRender(markup: string, extraSettings?: Settings): string {
const { parsing, rendering } = getNonNullSettings(extraSettings)
const document = this.parse(markup, parsing)

return this.render(document, rendering)
const document = this.parse(markup, extraSettings?.parsing)
return this.render(document, extraSettings?.rendering)
}

// This method converts Up markup into two pieces of HTML, both of which are returned:
// Converts Up markup into two pieces of HTML, both of which are returned:
//
// 1. A table of contents
// 2. The document itself
parseAndRenderWithTableOfContents(markup: string, extraSettings?: Settings): DocumentAndTableOfContentsHtml {
const { parsing, rendering } = getNonNullSettings(extraSettings)
const document = this.parse(markup, parsing)

return this.renderWithTableOfContents(document, rendering)
const document = this.parse(markup, extraSettings?.parsing)
return this.renderWithTableOfContents(document, extraSettings?.rendering)
}

// Converts inline Up markup into inline HTML and returns the result.
parseAndRenderInline(inlineMarkup: string, extraSettings?: Settings): string {
const { parsing, rendering } = getNonNullSettings(extraSettings)
const inlineDocument = this.parseInline(inlineMarkup, parsing)

return this.renderInline(inlineDocument, rendering)
const inlineDocument = this.parseInline(inlineMarkup, extraSettings?.parsing)
return this.renderInline(inlineDocument, extraSettings?.rendering)
}

// Parses Up markup and returns the resulting syntax tree.
parse(markup: string, extraParsingSettings?: Settings.Parsing): Document {
return parse(markup, this.getParsingSettings(extraParsingSettings))
parse(markup: string, extraSettings?: Settings.Parsing): Document {
return parse(markup, this.getParsingSettings(extraSettings))
}

// Parses inline Up markup and returns the resulting inline syntax tree.
parseInline(inlineMarkup: string, extraParsingSettings?: Settings.Parsing): InlineDocument {
return parseInline(inlineMarkup, this.getParsingSettings(extraParsingSettings))
parseInline(inlineMarkup: string, extraSettings?: Settings.Parsing): InlineDocument {
return parseInline(inlineMarkup, this.getParsingSettings(extraSettings))
}

// Converts a syntax tree into HTML, then returns the result.
render(document: Document, extraRenderingSettings?: Settings.Rendering): string {
const htmlRenderer = this.getHtmlRenderer(extraRenderingSettings)

render(document: Document, extraSettings?: Settings.Rendering): string {
const htmlRenderer = this.getHtmlRenderer(extraSettings)
return htmlRenderer.document(document)
}

// This method converts a syntax tree into two pieces of HTML, both of which are returned:
// Converts a syntax tree into two pieces of HTML, both of which are returned:
//
// 1. A table of contents
// 2. The document itself
renderWithTableOfContents(document: Document, extraRenderingSettings?: Settings.Rendering): DocumentAndTableOfContentsHtml {
const htmlRenderer = this.getHtmlRenderer(extraRenderingSettings)
renderWithTableOfContents(document: Document, extraSettings?: Settings.Rendering): DocumentAndTableOfContentsHtml {
const htmlRenderer = this.getHtmlRenderer(extraSettings)

return {
documentHtml: htmlRenderer.document(document),
Expand All @@ -72,14 +65,13 @@ export class Up {
}

// Converts an inline syntax tree into inline HTML and returns the result.
renderInline(inlineDocument: InlineDocument, extraRenderingSettings?: Settings.Rendering): string {
const htmlRenderer = this.getHtmlRenderer(extraRenderingSettings)

renderInline(inlineDocument: InlineDocument, extraSettings?: Settings.Rendering): string {
const htmlRenderer = this.getHtmlRenderer(extraSettings)
return htmlRenderer.inlineDocument(inlineDocument)
}

private getHtmlRenderer(extraRenderingSettings: Settings.Rendering | undefined): HtmlRenderer {
return new HtmlRenderer(this.getRenderingSettings(extraRenderingSettings))
private getHtmlRenderer(extraSettings: Settings.Rendering | undefined): HtmlRenderer {
return new HtmlRenderer(this.getRenderingSettings(extraSettings))
}

private getParsingSettings(changes: Settings.Parsing | undefined): NormalizedSettings.Parsing {
Expand All @@ -96,10 +88,3 @@ export interface DocumentAndTableOfContentsHtml {
documentHtml: string
tableOfContentsHtml: string
}

function getNonNullSettings(settings: Settings | undefined): Settings {
return settings ?? {
parsing: {},
rendering: {}
}
}
94 changes: 27 additions & 67 deletions src/Main.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,3 @@
import { Settings } from './Implementation/Settings'
import { Document } from './Implementation/SyntaxNodes/Document'
import { InlineDocument } from './Implementation/SyntaxNodes/InlineDocument'
import { DocumentAndTableOfContentsHtml, Up } from './Implementation/Up'


// The functions below allow developers to use Up without having to create any instances of
// the `Up` class.
//
// Thanks to these functions, it's never necessary to create instances of `Up`, though it's
// still sometimes more convenient.
//
// For example, let's say you're parsing an article and its comments. For each comment,
// you want to specify a unique ID prefix; for both the article and its comments, you
// want to use custom Japanese keywords.
//
// By creating an instance of `Up`, you can specify those custom Japanese keywords just once
// (in the constructor). Then, when parsing each comment, you only need to provide a unique
// ID prefix.
const up = new Up()

// Converts Up markup into HTML and returns the result.
export function parseAndRender(markup: string, settings?: Settings): string {
return up.parseAndRender(markup, settings)
}

// This function converts Up markup into two pieces of HTML, both of which are returned:
//
// 1. A table of contents
// 2. The document itself
export function parseAndRenderWithTableOfContents(markup: string, settings?: Settings): DocumentAndTableOfContentsHtml {
return up.parseAndRenderWithTableOfContents(markup, settings)
}

// Converts inline Up markup into inline HTML and returns the result.
export function parseAndRenderInline(inlineMarkup: string, settings?: Settings): string {
return up.parseAndRenderInline(inlineMarkup, settings)
}

// Parses Up markup and returns the resulting syntax tree.
export function parse(markup: string, parsingSettings?: Settings.Parsing): Document {
return up.parse(markup, parsingSettings)
}

// Parses inline Up markup and returns the resulting inline syntax tree.
export function parseInline(inlineMarkup: string, parsingSettings?: Settings.Parsing): InlineDocument {
return up.parseInline(inlineMarkup, parsingSettings)
}

// Converts a syntax tree into HTML, then returns the result.
export function render(document: Document, renderingSettings?: Settings.Rendering): string {
return up.render(document, renderingSettings)
}

// This function converts a syntax tree into two pieces of HTML, both of which are returned:
//
// 1. A table of contents
// 2. The document itself
export function renderWithTableOfContents(document: Document, renderingSettings?: Settings.Rendering): DocumentAndTableOfContentsHtml {
return up.renderWithTableOfContents(document, renderingSettings)
}

// Converts an inline syntax tree into inline HTML and returns the result.
export function renderInline(inlineDocument: InlineDocument, renderingSettings?: Settings.Rendering): string {
return up.renderInline(inlineDocument, renderingSettings)
}

export { Up, DocumentAndTableOfContentsHtml } from './Implementation/Up'
export { Settings } from './Implementation/Settings'

Expand Down Expand Up @@ -115,3 +48,30 @@ export { SyntaxNode } from './Implementation/SyntaxNodes/SyntaxNode'

// This must always match the `version` field in `package.json`.
export const VERSION = '39.0.0'

// Below, we export bound functions to allow developers to use Up without having to create
// any instances of the `Up` class.
//
// Thanks to these functions, it's never necessary to create instances of `Up`, though it
// can sometimes be more convenient.
//
// For example, let's say you're parsing an article and its comments. For each comment,
// you want to specify a unique ID prefix; for both the article and its comments, you
// want to use custom Japanese keywords.
//
// By creating an instance of `Up`, you can specify those custom Japanese keywords just once
// (in the constructor). Then, when parsing each comment, you only need to provide a unique
// ID prefix.

import { Up } from './Implementation/Up'

const up = new Up();

export const parseAndRender = up.parseAndRender.bind(up)
export const parseAndRenderWithTableOfContents = up.parseAndRenderWithTableOfContents.bind(up)
export const parseAndRenderInline = up.parseAndRenderInline.bind(up)
export const parse = up.parse.bind(up)
export const parseInline = up.parseInline.bind(up)
export const render = up.render.bind(up)
export const renderWithTableOfContents = up.renderWithTableOfContents.bind(up)
export const renderInline = up.renderInline.bind(up)

0 comments on commit c47eb66

Please sign in to comment.