|
| 1 | +/* |
| 2 | + * @flow strict-local |
| 3 | + * Copyright (C) 2021 MetaBrainz Foundation |
| 4 | + * |
| 5 | + * This file is part of MusicBrainz, the open internet music database, |
| 6 | + * and is licensed under the GPL version 2, or (at your option) any |
| 7 | + * later version: http://www.gnu.org/licenses/gpl-2.0.txt |
| 8 | + */ |
| 9 | + |
| 10 | +import * as React from 'react'; |
| 11 | + |
| 12 | +function setlistLink(entityType, entityGid, content) { |
| 13 | + let formattedContent = content; |
| 14 | + if (!nonEmpty(formattedContent)) { |
| 15 | + formattedContent = entityType + ':' + entityGid; |
| 16 | + } |
| 17 | + |
| 18 | + return ( |
| 19 | + <a href={`/${entityType}/${entityGid}`}> |
| 20 | + {formattedContent} |
| 21 | + </a> |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +const linkRegExp = |
| 26 | + /^\[([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})(?:\|([^\]]+))?\]/i; |
| 27 | + |
| 28 | +function formatSetlistArtist(entityGid, content) { |
| 29 | + return ( |
| 30 | + <strong> |
| 31 | + {addColonText(l('Artist'))} |
| 32 | + {' '} |
| 33 | + {setlistLink('artist', entityGid, content)} |
| 34 | + </strong> |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +function formatSetlistWork(entityGid, content) { |
| 39 | + return setlistLink('work', entityGid, content); |
| 40 | +} |
| 41 | + |
| 42 | +export default function formatSetlist( |
| 43 | + setlistText: string, |
| 44 | +): Expand2ReactOutput { |
| 45 | + const rawLines = setlistText.split(/(?:\r\n|\n\r|\r|\n)/); |
| 46 | + const elements = []; |
| 47 | + |
| 48 | + for (const rawLine of rawLines) { |
| 49 | + if (!nonEmpty(rawLine)) { |
| 50 | + elements.push(<br />); |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + const symbol = rawLine.substring(0, 2); |
| 55 | + const line = rawLine.substring(2); |
| 56 | + let entityType; |
| 57 | + |
| 58 | + switch (symbol) { |
| 59 | + // Lines starting with @ are artists |
| 60 | + case '@ ': |
| 61 | + entityType = 'artist'; |
| 62 | + break; |
| 63 | + |
| 64 | + // Lines starting with * are works |
| 65 | + case '* ': |
| 66 | + entityType = 'work'; |
| 67 | + break; |
| 68 | + |
| 69 | + // Lines starting with # are comments |
| 70 | + case '# ': |
| 71 | + elements.push(<span className="comment">{line}</span>); |
| 72 | + break; |
| 73 | + |
| 74 | + // Lines that don't start with a symbol are ignored |
| 75 | + } |
| 76 | + |
| 77 | + if (entityType) { |
| 78 | + const startingBracketRegExp = /\[/g; |
| 79 | + |
| 80 | + let match; |
| 81 | + let lastIndex = 0; |
| 82 | + |
| 83 | + while ((match = startingBracketRegExp.exec(line))) { |
| 84 | + const textBeforeMatch = line.substring(lastIndex, match.index); |
| 85 | + elements.push(textBeforeMatch); |
| 86 | + lastIndex = match.index; |
| 87 | + |
| 88 | + const remainder = line.substring(match.index); |
| 89 | + const linkMatch = remainder.match(linkRegExp); |
| 90 | + |
| 91 | + if (linkMatch) { |
| 92 | + const [linkMatchText, entityGid, content] = linkMatch; |
| 93 | + switch (entityType) { |
| 94 | + case 'artist': |
| 95 | + elements.push(formatSetlistArtist(entityGid, content)); |
| 96 | + break; |
| 97 | + case 'work': |
| 98 | + elements.push(formatSetlistWork(entityGid, content)); |
| 99 | + break; |
| 100 | + } |
| 101 | + lastIndex += linkMatchText.length; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + elements.push(line.substring(lastIndex)); |
| 106 | + } |
| 107 | + |
| 108 | + elements.push(<br />); |
| 109 | + } |
| 110 | + |
| 111 | + return React.createElement(React.Fragment, null, ...elements); |
| 112 | +} |
0 commit comments