Skip to content

Commit

Permalink
Allow more tag types (#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevgo authored Apr 20, 2018
1 parent ffc156a commit f57bf8e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
21 changes: 21 additions & 0 deletions features/block-syntax/html-tags.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Feature: support for more HTML tags

When writing active blocks in a Markdown document
I want to be able to make a variety of HTML tags active
So that I don't have to litter my document with <a> tags


Background:
Given my workspace contains the HelloWorld activity


Scenario: H1 tag
Given my source code contains the file "1.md" with content:
"""
<h1 textrun="HelloWorld">hello</h1>
"""
When running text-run
Then it signals:
| FILENAME | 1.md |
| LINE | 1 |
| MESSAGE | hello world |
29 changes: 21 additions & 8 deletions src/commands/run/activity-list-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ActivityListBuilder {
formatter: Formatter
linkTargets: LinkTargetList
regex: RegExp
activeTagType: string
tagTypeRegex: RegExp

constructor (value: {
activityTypesManager: ActivityTypeManager,
Expand All @@ -38,10 +40,11 @@ class ActivityListBuilder {
this.regex = new RegExp(
` ${this.configuration.get('classPrefix')}="([^"]+)"`
)
this.tagTypeRegex = /<(\w+).*>/
}

build (tree: AstNodeList): ActivityList {
var insideActiveBlock = false // whether we are currently processing nodes of an active block
this.activeTagType = '' // whether we are currently processing nodes of an active block
var nodesForCurrentRunner: AstNodeList = []

// contains the most recent line in the file that we are aware of
Expand All @@ -52,14 +55,14 @@ class ActivityListBuilder {
for (let node: AstNode of tree) {
const isActiveBlockStartTag = this._determineIsActiveBlockStartTag(node)
if (isActiveBlockStartTag) {
if (insideActiveBlock) {
if (this.activeTagType !== '') {
throw new UnprintedUserError(
`Block ${node.content || ''} is nested in another 'textrun' block.`,
this.filePath,
line
)
}
insideActiveBlock = true
this.activeTagType = this._getTagType(node)
if (node.line != null) {
line = node.line
}
Expand All @@ -73,7 +76,7 @@ class ActivityListBuilder {
}

if (this._isActiveBlockEndTag(node)) {
if (insideActiveBlock) {
if (this.activeTagType !== '') {
result.push({
filename: this.filePath,
activityTypeName: this._convertIntoActivityTypeName(blockType),
Expand All @@ -86,12 +89,12 @@ class ActivityListBuilder {
searcher: new Searcher(nodesForCurrentRunner)
})
}
insideActiveBlock = false
this.activeTagType = ''
nodesForCurrentRunner = []
continue
}

if (insideActiveBlock) {
if (this.activeTagType !== '') {
nodesForCurrentRunner.push(node)
continue
}
Expand Down Expand Up @@ -159,6 +162,14 @@ class ActivityListBuilder {
return result
}

_getTagType (node: AstNode): string {
const matches = node.content && node.content.match(this.tagTypeRegex)
if (!matches) {
return ''
}
return matches[1]
}

_htmlLinkTarget (node: AstNode): ?string {
if (node.content == null) return null
const matches = node.content.match(/<a[^>]*href="([^"]*)".*?>/)
Expand Down Expand Up @@ -192,8 +203,10 @@ class ActivityListBuilder {
}

// Returns whether the given node is the end of an active block
_isActiveBlockEndTag (node) {
return node.type === 'htmltag' && node.content === '</a>'
_isActiveBlockEndTag (node: AstNode) {
return (
node.type === 'htmltag' && node.content === `</${this.activeTagType}>`
)
}
}

Expand Down

0 comments on commit f57bf8e

Please sign in to comment.