pomljs
Version:
Prompt Orchestration Markup Language
211 lines (205 loc) • 8.75 kB
JavaScript
var React = require('react');
var base = require('../base.cjs');
var essentials = require('../essentials.cjs');
function _interopNamespaceDefault(e) {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () { return e[k]; }
});
}
});
}
n.default = e;
return Object.freeze(n);
}
var React__namespace = /*#__PURE__*/_interopNamespaceDefault(React);
// Helper components for caption text.
// Try not to use it in syntaxes other than markup.
const CaptionText = base.component('CaptionText')((props) => {
const { caption, captionTextTransform = 'none', captionStyle = 'header', captionEnding, ...others } = props;
let captionText = caption;
switch (captionTextTransform) {
case 'upper':
captionText = captionText.toUpperCase();
break;
case 'lower':
captionText = captionText.toLowerCase();
break;
case 'capitalize':
captionText =
captionText.length >= 1
? captionText.charAt(0).toUpperCase() + captionText.slice(1)
: captionText;
break;
case 'none':
break;
default:
throw base.ReadError.fromProps(`Unsupported caption text transform: ${captionTextTransform}`, others);
}
const computedCaptionEnding = captionEnding === undefined
? captionStyle === 'bold' || captionStyle === 'plain'
? 'colon'
: 'none'
: captionEnding;
if (computedCaptionEnding.includes('colon')) {
return React__namespace.createElement(React__namespace.Fragment, null,
captionText,
":");
}
else {
return React__namespace.createElement(React__namespace.Fragment, null, captionText);
}
});
const Caption = base.component('Caption', {
requiredProps: ['caption']
})((props) => {
const presentation = essentials.computeSyntaxContext(props);
if (presentation === 'markup') {
const { caption, captionStyle = 'header', captionEnding, captionTailingSpace, ...others } = props;
if (captionStyle === 'header') {
return (React__namespace.createElement(essentials.Header, { ...others },
React__namespace.createElement(CaptionText, { caption: caption, captionStyle: captionStyle, captionEnding: captionEnding, ...others })));
}
else if (captionStyle === 'bold') {
const result = (React__namespace.createElement(essentials.Bold, { ...others },
React__namespace.createElement(CaptionText, { caption: caption, captionStyle: captionStyle, captionEnding: captionEnding, ...others })));
if (captionTailingSpace === undefined || captionTailingSpace) {
if (captionEnding?.includes('newline')) {
return (React__namespace.createElement(React__namespace.Fragment, null,
result,
React__namespace.createElement(essentials.Newline, null)));
}
else {
return React__namespace.createElement(React__namespace.Fragment, null,
result,
" ");
}
}
else {
return result;
}
}
else if (captionStyle === 'plain') {
const result = (React__namespace.createElement(essentials.Inline, null,
React__namespace.createElement(CaptionText, { caption: caption, captionStyle: captionStyle, captionEnding: captionEnding, ...others })));
if (captionTailingSpace === undefined || captionTailingSpace) {
if (captionEnding?.includes('newline')) {
return (React__namespace.createElement(React__namespace.Fragment, null,
result,
React__namespace.createElement(essentials.Newline, null)));
}
else {
return React__namespace.createElement(React__namespace.Fragment, null,
result,
" ");
}
}
else {
return result;
}
}
else {
return null;
}
}
else {
// this should seldom happen
const { caption, captionSerialized, name, type, ...others } = props;
if (presentation === 'serialize') {
return (React__namespace.createElement(essentials.Text, { name: name, type: type, ...others }, captionSerialized));
}
else {
return React__namespace.createElement(essentials.Text, null, caption);
}
}
});
/**
* CaptionedParagraph (`<cp>` for short) creates a paragraph with a customized caption title.
*
* @param caption - The title or label for the paragraph. Required.
* @param captionSerialized - The serialized version of the caption when using "serializer" syntaxes.
* By default, it's same as `caption`.
* @param {'header'|'bold'|'plain'|'hidden'} captionStyle - Determines the style of the caption,
* applicable only for "markup" syntaxes. Default is `header`.
* @param {'upper'|'level'|'capitalize'|'none'} captionTextTransform -
* Specifies text transformation for the caption, applicable only for "markup" syntaxes. Default is `none`.
* @param {'colon'|'newline'|'colon-newline'|'none'} captionEnding - A caption can ends with a colon, a newline or simply nothing.
* If not specified, it defaults to `colon` for `bold` or `plain` captionStyle, and `none` otherwise.
*
* @see {@link Paragraph} for other props available.
*
* @example
* ```xml
* <cp caption="Constraints">
* <list>
* <item>Do not exceed 1000 tokens.</item>
* <item>Please use simple words.</item>
* </list>
* </cp>
* ```
*/
const CaptionedParagraph = base.component('CaptionedParagraph', {
aliases: ['cp'],
requiredProps: ['caption']
})((props) => {
const presentation = essentials.computeSyntaxContext(props);
if (presentation === 'markup') {
const { captionStyle = 'header', children, ...others } = props;
const trimmedChildren = base.trimChildrenWhiteSpace(children, props);
const hasContent = React__namespace.Children.count(trimmedChildren) > 0;
if (captionStyle === 'header') {
return (React__namespace.createElement(essentials.Paragraph, { ...others },
React__namespace.createElement(Caption, { captionStyle: captionStyle, captionTailingSpace: hasContent, ...others }),
React__namespace.createElement(essentials.SubContent, null, trimmedChildren)));
}
else if (captionStyle === 'bold' || captionStyle === 'plain') {
return (React__namespace.createElement(essentials.Paragraph, { ...others },
React__namespace.createElement(Caption, { captionStyle: captionStyle, captionTailingSpace: hasContent, ...others }),
trimmedChildren));
}
else if (captionStyle === 'hidden') {
return React__namespace.createElement(essentials.Paragraph, { ...others }, trimmedChildren);
}
else {
throw base.ReadError.fromProps(`Unsupported caption style: ${captionStyle}`, props);
}
}
else if (presentation === 'serialize') {
const { children, captionSerialized, caption, ...others } = props;
return (React__namespace.createElement(essentials.Text, { name: captionSerialized || caption, ...others }, children));
}
else {
const { children } = props;
return React__namespace.createElement(essentials.Text, null, children);
}
});
const parsePythonStyleSlice = (slice, totalLength) => {
// slices could be like :3, 3:5, 5:, 5:-1
if (slice === ':') {
return [0, totalLength];
}
else if (slice.endsWith(':')) {
return [parseInt(slice.slice(0, -1)), totalLength];
}
else if (slice.startsWith(':')) {
return [0, parseInt(slice.slice(1))];
}
else if (slice.includes(':')) {
const [start, end] = slice.split(':').map(Number);
return [start, end];
}
else {
const index = parseInt(slice);
return [index, index + 1];
}
};
exports.Caption = Caption;
exports.CaptionedParagraph = CaptionedParagraph;
exports.parsePythonStyleSlice = parsePythonStyleSlice;
//# sourceMappingURL=utils.cjs.map
;