Loading generateBaseline/mermaid-filter.js +30 −6 Changes for generateBaseline/mermaid-filter.js: 30 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -115,22 +115,46 @@ function renderMermaid(el, caption) { } catch (e) { /* ignore */ } const captionInlines = Array.isArray(caption) ? caption : []; const fig = (id.startsWith('fig:') || captionInlines.length) ? 'fig:' : ''; const image = { t: 'Image', c: [[id, [], []], captionInlines, [src, captionInlines.length ? 'fig:' : '']] }; if (!captionInlines.length) { return { t: 'Para', c: [{ t: 'Image', c: [[id, [], []], captionInlines, [src, fig]] }] c: [image] }; } // Pandoc 3+ docx writer only styles a caption for Figure blocks. return { t: 'Figure', c: [ [id, [], []], [null, [{ t: 'Plain', c: captionInlines }]], [{ t: 'Plain', c: [image] }] ] }; } function captionFromAttrs(el) { const kvs = el && el.c && el.c[0] && el.c[0][2]; if (!Array.isArray(kvs)) { return []; } for (let i = 0; i < kvs.length; i++) { if (kvs[i] && kvs[i][0] === 'caption' && kvs[i][1]) { return [{ t: 'Str', c: String(kvs[i][1]) }]; } } return []; } function walkBlocks(blocks) { const out = []; for (let i = 0; i < blocks.length; i++) { const el = blocks[i]; if (isMermaidBlock(el)) { let caption = []; let caption = captionFromAttrs(el); if (isFigureCaptionPara(blocks[i + 1])) { caption = figureCaptionInlines(blocks[i + 1]); i += 1; Loading generateBaseline/pandocFilter.py +6 −31 Changes for generateBaseline/pandocFilter.py: 6 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -274,31 +274,6 @@ def replaceTableCaptions(progress:Progress, mdLines:list[str]) -> list[str]: return _lines def _opens_mermaid_fence(line: str) -> bool: stripped = line.strip() if not stripped.startswith('```'): return False info = stripped[3:].strip() lang = info.split()[0] if info else '' return lang == 'mermaid' or 'mermaid' in lang def _follows_mermaid_block(lines: list[str]) -> bool: """True when the last non-empty line closes a ```mermaid fence.""" idx = len(lines) - 1 while idx >= 0 and lines[idx].strip() == '': idx -= 1 if idx < 0 or lines[idx].strip() != '```': return False idx -= 1 while idx >= 0: stripped = lines[idx].strip() if stripped.startswith('```'): return _opens_mermaid_fence(lines[idx]) idx -= 1 return False def replaceFigureCaptions(progress:Progress, mdLines:list[str]) -> list[str]: """ Replace figure captions with a pandoc figure caption. """ Loading @@ -315,16 +290,16 @@ def replaceFigureCaptions(progress:Progress, mdLines:list[str]) -> list[str]: _lines.append(line) continue if matches: # Leave mermaid captions in place for mermaid-filter (no ![ ] yet). if _follows_mermaid_block(_lines): _lines.append(line) continue # Replace the previous figure markdown name with the captions # Stop if image reference is found, or if the line is not empty _idx = len(_lines) - 1 while _idx >= 0 and _lines[_idx].count('![') == 0: while _idx >= 0 and _lines[_idx].count('![') == 0 and _lines[_idx].strip() == '': _idx -= 1 if _idx > 0: if _idx > 0 and _lines[_idx].count('![') > 0: _lines[_idx] = re.sub(r'!.*]', f'![{matches[0]}]', _lines[_idx]) else: _lines.append(line) print(f'Corresponding figure not found for caption: {matches[0]}, figure caption is not removed') elif re.findall(nocaptionfigureregex, line): _lines.append(re.sub(r'^.*?]', f'![]', line)) else: Loading Loading
generateBaseline/mermaid-filter.js +30 −6 Changes for generateBaseline/mermaid-filter.js: 30 added lines, 6 removed lines. Original line number Diff line number Diff line Loading @@ -115,22 +115,46 @@ function renderMermaid(el, caption) { } catch (e) { /* ignore */ } const captionInlines = Array.isArray(caption) ? caption : []; const fig = (id.startsWith('fig:') || captionInlines.length) ? 'fig:' : ''; const image = { t: 'Image', c: [[id, [], []], captionInlines, [src, captionInlines.length ? 'fig:' : '']] }; if (!captionInlines.length) { return { t: 'Para', c: [{ t: 'Image', c: [[id, [], []], captionInlines, [src, fig]] }] c: [image] }; } // Pandoc 3+ docx writer only styles a caption for Figure blocks. return { t: 'Figure', c: [ [id, [], []], [null, [{ t: 'Plain', c: captionInlines }]], [{ t: 'Plain', c: [image] }] ] }; } function captionFromAttrs(el) { const kvs = el && el.c && el.c[0] && el.c[0][2]; if (!Array.isArray(kvs)) { return []; } for (let i = 0; i < kvs.length; i++) { if (kvs[i] && kvs[i][0] === 'caption' && kvs[i][1]) { return [{ t: 'Str', c: String(kvs[i][1]) }]; } } return []; } function walkBlocks(blocks) { const out = []; for (let i = 0; i < blocks.length; i++) { const el = blocks[i]; if (isMermaidBlock(el)) { let caption = []; let caption = captionFromAttrs(el); if (isFigureCaptionPara(blocks[i + 1])) { caption = figureCaptionInlines(blocks[i + 1]); i += 1; Loading
generateBaseline/pandocFilter.py +6 −31 Changes for generateBaseline/pandocFilter.py: 6 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -274,31 +274,6 @@ def replaceTableCaptions(progress:Progress, mdLines:list[str]) -> list[str]: return _lines def _opens_mermaid_fence(line: str) -> bool: stripped = line.strip() if not stripped.startswith('```'): return False info = stripped[3:].strip() lang = info.split()[0] if info else '' return lang == 'mermaid' or 'mermaid' in lang def _follows_mermaid_block(lines: list[str]) -> bool: """True when the last non-empty line closes a ```mermaid fence.""" idx = len(lines) - 1 while idx >= 0 and lines[idx].strip() == '': idx -= 1 if idx < 0 or lines[idx].strip() != '```': return False idx -= 1 while idx >= 0: stripped = lines[idx].strip() if stripped.startswith('```'): return _opens_mermaid_fence(lines[idx]) idx -= 1 return False def replaceFigureCaptions(progress:Progress, mdLines:list[str]) -> list[str]: """ Replace figure captions with a pandoc figure caption. """ Loading @@ -315,16 +290,16 @@ def replaceFigureCaptions(progress:Progress, mdLines:list[str]) -> list[str]: _lines.append(line) continue if matches: # Leave mermaid captions in place for mermaid-filter (no ![ ] yet). if _follows_mermaid_block(_lines): _lines.append(line) continue # Replace the previous figure markdown name with the captions # Stop if image reference is found, or if the line is not empty _idx = len(_lines) - 1 while _idx >= 0 and _lines[_idx].count('![') == 0: while _idx >= 0 and _lines[_idx].count('![') == 0 and _lines[_idx].strip() == '': _idx -= 1 if _idx > 0: if _idx > 0 and _lines[_idx].count('![') > 0: _lines[_idx] = re.sub(r'!.*]', f'![{matches[0]}]', _lines[_idx]) else: _lines.append(line) print(f'Corresponding figure not found for caption: {matches[0]}, figure caption is not removed') elif re.findall(nocaptionfigureregex, line): _lines.append(re.sub(r'^.*?]', f'![]', line)) else: Loading