Skip to content

Commit

Permalink
fix(Code): Refactor code related classes
Browse files Browse the repository at this point in the history
This makes breaking changes to the code related nodes:

- refactor `Code` to be a class for all code related classes without the bloat previously associated with using `SoftwareSourceCode` (previously used for `CodeExpr` and `CodeChunk`)

- add a `CodeFragment` type for inline code (using `Code` as a base class and for inline code caused issues with type maps)

- rename `CodeExpr` to `CodeExpression` for consistency with full-word-ness of `CodeFragment`

Closes #92
  • Loading branch information
nokome authored and beneboy committed Sep 2, 2019
1 parent f58657b commit deb1c51
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 45 deletions.
10 changes: 5 additions & 5 deletions schema/Code.schema.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: Code
'@id': stencila:Code
extends: Thing
extends: Entity
role: secondary
status: stable
category: code
Expand All @@ -10,9 +10,9 @@ properties:
'@id': schema:programmingLanguage
description: The programming language of the code.
type: string
value:
'@id': schema:value
description: The text value.
text:
'@id': schema:text
description: The text of the code.
type: string
required:
- value
- text
2 changes: 1 addition & 1 deletion schema/CodeChunk.schema.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: CodeChunk
'@id': stencila:CodeChunk
extends: SoftwareSourceCode
extends: CodeBlock
role: secondary
status: unstable
category: code
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
title: CodeExpr
'@id': stencila:CodeExpr
extends: SoftwareSourceCode
title: CodeExpression
'@id': stencila:CodeExpression
extends: CodeFragment
role: secondary
status: unstable
status: stable
category: code
description: An expression defined in programming language source code.
properties:
value:
'@id': schema:value
output:
'@id': stencila:output
description: The value of the expression when it was last evaluated.
allOf:
- $ref: Node
8 changes: 8 additions & 0 deletions schema/CodeFragment.schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: CodeFragment
'@id': stencila:CodeFragment
extends: Code
role: secondary
status: stable
category: code
description: Inline code.
properties: {}
2 changes: 0 additions & 2 deletions schema/CreativeWorkTypes.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ anyOf:
- $ref: CreativeWork
- $ref: Article
- $ref: AudioObject
- $ref: CodeChunk
- $ref: CodeExpr
- $ref: Collection
- $ref: Datatable
- $ref: Figure
Expand Down
4 changes: 2 additions & 2 deletions schema/InlineContent.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ anyOf:
- type: integer
- type: number
- type: string
- $ref: Code
- $ref: CodeExpr
- $ref: CodeFragment
- $ref: CodeExpression
- $ref: Delete
- $ref: Emphasis
- $ref: ImageObject
Expand Down
52 changes: 23 additions & 29 deletions ts/util/type-maps.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import {
BlockContent,
Code,
CreativeWork,
Delete,
Emphasis,
InlineContent,
Quote,
Strong,
Subscript,
Superscript
} from '../types'
import * as types from '../types'
import { TypeMap } from './type-map'

export type MarkTypes =
| Delete
| Emphasis
| Quote
| Strong
| Subscript
| Superscript
| types.Delete
| types.Emphasis
| types.Quote
| types.Strong
| types.Subscript
| types.Superscript

export const markTypes: TypeMap<MarkTypes> = {
Delete: 'Delete',
Expand All @@ -30,16 +19,15 @@ export const markTypes: TypeMap<MarkTypes> = {
}

export type InlineNodesWithType = Exclude<
InlineContent,
types.InlineContent,
string | null | boolean | number
>

export const inlineContentTypes: TypeMap<InlineNodesWithType> = {
Cite: 'Cite',
CiteGroup: 'CiteGroup',
Code: 'Code',
CodeBlock: 'CodeBlock',
CodeExpr: 'CodeExpr',
CodeFragment: 'CodeFragment',
CodeExpression: 'CodeExpression',
Delete: 'Delete',
Emphasis: 'Emphasis',
ImageObject: 'ImageObject',
Expand All @@ -50,7 +38,7 @@ export const inlineContentTypes: TypeMap<InlineNodesWithType> = {
Superscript: 'Superscript'
}

export const blockContentTypes: TypeMap<BlockContent> = {
export const blockContentTypes: TypeMap<types.BlockContent> = {
CodeBlock: 'CodeBlock',
CodeChunk: 'CodeChunk',
Heading: 'Heading',
Expand All @@ -62,12 +50,10 @@ export const blockContentTypes: TypeMap<BlockContent> = {
ThematicBreak: 'ThematicBreak'
}

export const creativeWorkTypes: TypeMap<CreativeWork> = {
export const creativeWorkTypes: TypeMap<types.CreativeWork> = {
CreativeWork: 'CreativeWork',
Article: 'Article',
AudioObject: 'AudioObject',
CodeChunk: 'CodeChunk',
CodeExpr: 'CodeExpr',
Collection: 'Collection',
Datatable: 'Datatable',
ImageObject: 'ImageObject',
Expand All @@ -82,7 +68,15 @@ export const creativeWorkTypes: TypeMap<CreativeWork> = {
VideoObject: 'VideoObject'
}

export const codeTypes: TypeMap<Code> = {
Code: 'Code',
CodeBlock: 'CodeBlock'
export type CodeTypes =
| types.CodeFragment
| types.CodeExpression
| types.CodeBlock
| types.CodeChunk

export const codeTypes: TypeMap<CodeTypes> = {
CodeFragment: 'CodeFragment',
CodeExpression: 'CodeExpression',
CodeBlock: 'CodeBlock',
CodeChunk: 'CodeChunk'
}

0 comments on commit deb1c51

Please sign in to comment.