Skip to content

Commit

Permalink
Desktop,Cli: enex_to_md: support bold in span tags using inline styles
Browse files Browse the repository at this point in the history
* function isSpanWithStyle() checks if further processing of a span tag
  makes sense
* function isSpanStyleBold() checks if bold formatting via styles is
  used - a similar function could be written for each span-inline-style-format
  that should be supported
  • Loading branch information
JOJ0 committed Jul 9, 2019
1 parent 440e1f1 commit 7031753
Showing 1 changed file with 42 additions and 2 deletions.
44 changes: 42 additions & 2 deletions ReactNativeClient/lib/import-enex-md-gen.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ function isAnchor(n) {
}

function isIgnoredEndTag(n) {
return ["en-note", "en-todo", "span", "body", "html", "font", "br", 'hr', 'tbody', 'sup', 'img', 'abbr', 'cite', 'thead', 'small', 'tt', 'sub', 'colgroup', 'col', 'ins', 'caption', 'var', 'map', 'area'].indexOf(n) >= 0;
return ["en-note", "en-todo", "body", "html", "font", "br", 'hr', 'tbody', 'sup', 'img', 'abbr', 'cite', 'thead', 'small', 'tt', 'sub', 'colgroup', 'col', 'ins', 'caption', 'var', 'map', 'area'].indexOf(n) >= 0;
}

function isListTag(n) {
Expand Down Expand Up @@ -394,6 +394,30 @@ function attributeToLowerCase(node) {
return output;
}

function isSpanWithStyle(attributes, state) {
if (attributes != undefined) {
if ('style' in attributes) {
state.spanAttributes.push(attributes);
return true;
} else {
return false;
}
}
}

function isSpanStyleBold(attributes) {
let style = attributes.style;
if (style.includes('font-weight: bold;')) {
return true;
} else if (style.search( /font-family:.*,Bold.*;/ ) != -1) {
//console.debug('font-family regex matched');
return true;
} else {
//console.debug('Found unsupported style(s) in span tag: %s', style);
return false;
}
}

function enexXmlToMdArray(stream, resources) {
let remainingResources = resources.slice();

Expand All @@ -413,6 +437,7 @@ function enexXmlToMdArray(stream, resources) {
inQuote: false,
lists: [],
anchorAttributes: [],
spanAttributes: [],
};

let options = {};
Expand Down Expand Up @@ -679,7 +704,14 @@ function enexXmlToMdArray(stream, resources) {
if (resource && !!resource.id) {
section.lines = addResourceTag(section.lines, resource, nodeAttributes.alt);
}
} else if (["span", "font", 'sup', 'cite', 'abbr', 'small', 'tt', 'sub', 'colgroup', 'col', 'ins', 'caption', 'var', 'map', 'area'].indexOf(n) >= 0) {
} else if (n == "span") {
if (isSpanWithStyle(nodeAttributes, state)) {
if (isSpanStyleBold(nodeAttributes)) {
//console.debug('Applying style found in span tag: bold')
section.lines.push("**");
}
}
} else if (["font", 'sup', 'cite', 'abbr', 'small', 'tt', 'sub', 'colgroup', 'col', 'ins', 'caption', 'var', 'map', 'area'].indexOf(n) >= 0) {
// Inline tags that can be ignored in Markdown
} else {
console.warn("Unsupported start tag: " + n);
Expand Down Expand Up @@ -860,6 +892,14 @@ function enexXmlToMdArray(stream, resources) {
state.lists.pop();
} else if (n == "en-media") {
// Skip
} else if (n == 'span') {
let attributes = state.spanAttributes.pop();
if (isSpanWithStyle(attributes, state)) {

This comment has been minimized.

Copy link
@laurent22

laurent22 Jul 13, 2019

I'm not sure the logic is right here. It's poping the attributes but then it calls isSpanWithStyle which is again going to add back the attributes, which I assume it shouldn't. Could you clarify the behaviour here?

This comment has been minimized.

Copy link
@JOJ0

JOJ0 Jul 17, 2019

Author Owner

Hi Laurent, thanks for the hint! It did work though but was a mistake. I moved the pushing out of the function. Now it's only done when an opening span tag is found and not in the closing one. See new commit.

if (isSpanStyleBold(attributes)) {
//console.debug('Applying style found in span tag (closing): bold')
section.lines.push("**");
}
}
} else if (isIgnoredEndTag(n)) {
// Skip
} else {
Expand Down

0 comments on commit 7031753

Please sign in to comment.