Skip to content

Commit

Permalink
Merge pull request #9579 from ballerina-platform/automate-bbes-b8e978…
Browse files Browse the repository at this point in the history
…7aff356a51c34316fea796e2628abbe2ab

[Automated] Update Ballerina By Example(BBEs) pages
  • Loading branch information
sm1990 authored Oct 28, 2024
2 parents b8e9787 + 812c458 commit 0a6f0a3
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 13 deletions.
95 changes: 95 additions & 0 deletions swan-lake/by-example/configuring-via-toml/content.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,17 @@ public function main() {
io:println("admin details: ", admin);
io:println("users: ", users);
}
`,
`maxPayload = 3.21
acceptTypes = ["application/xml", "application/json", "text/plain"]
admin = {username = "admin", password = "password"}
users = [{username = "John", password = "abc123"}, {username = "Bob", password = "cde456"}]
`,
];

export function ConfiguringViaToml({ codeSnippets }) {
const [codeClick1, updateCodeClick1] = useState(false);
const [codeClick2, updateCodeClick2] = useState(false);

const [outputClick1, updateOutputClick1] = useState(false);
const ref1 = createRef();
Expand Down Expand Up @@ -161,6 +167,95 @@ export function ConfiguringViaToml({ codeSnippets }) {
</Col>
</Row>

<p>
To run the example, copy the following content to a file named{" "}
<code>Config.toml</code> in the current directory.
</p>

<Row
className="bbeCode mx-0 py-0 rounded
"
style={{ marginLeft: "0px" }}
>
<Col className="d-flex align-items-start" sm={12}>
<button
className="bg-transparent border-0 m-0 p-2 ms-auto"
onClick={() => {
window.open(
"https://github.com/ballerina-platform/ballerina-distribution/tree/v2201.10.2/examples/configuring-via-toml",
"_blank",
);
}}
aria-label="Edit on Github"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="#000"
className="bi bi-github"
viewBox="0 0 16 16"
>
<title>Edit on Github</title>
<path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z" />
</svg>
</button>
{codeClick2 ? (
<button
className="bg-transparent border-0 m-0 p-2"
disabled
aria-label="Copy to Clipboard Check"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="#20b6b0"
className="bi bi-check"
viewBox="0 0 16 16"
>
<title>Copied</title>
<path d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z" />
</svg>
</button>
) : (
<button
className="bg-transparent border-0 m-0 p-2"
onClick={() => {
updateCodeClick2(true);
copyToClipboard(codeSnippetData[1]);
setTimeout(() => {
updateCodeClick2(false);
}, 3000);
}}
aria-label="Copy to Clipboard"
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
fill="#000"
className="bi bi-clipboard"
viewBox="0 0 16 16"
>
<title>Copy to Clipboard</title>
<path d="M4 1.5H3a2 2 0 0 0-2 2V14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V3.5a2 2 0 0 0-2-2h-1v1h1a1 1 0 0 1 1 1V14a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3.5a1 1 0 0 1 1-1h1v-1z" />
<path d="M9.5 1a.5.5 0 0 1 .5.5v1a.5.5 0 0 1-.5.5h-3a.5.5 0 0 1-.5-.5v-1a.5.5 0 0 1 .5-.5h3zm-3-1A1.5 1.5 0 0 0 5 1.5v1A1.5 1.5 0 0 0 6.5 4h3A1.5 1.5 0 0 0 11 2.5v-1A1.5 1.5 0 0 0 9.5 0h-3z" />
</svg>
</button>
)}
</Col>
<Col sm={12}>
{codeSnippets[1] != undefined && (
<div
dangerouslySetInnerHTML={{
__html: DOMPurify.sanitize(codeSnippets[1]),
}}
/>
)}
</Col>
</Row>

<Row
className="bbeOutput mx-0 py-0 rounded "
style={{ marginLeft: "0px" }}
Expand Down
47 changes: 34 additions & 13 deletions swan-lake/by-example/regexp-type/content.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ import ballerina/lang.regexp;
public function main() returns error? {
// Declare a RegExp using regular expression literal.
string:RegExp _ = re \`[a-zA-Z0-9]\`;
// Declare a \`RegExp\` value using a regular expression literal.
regexp:RegExp alphanumericPattern = re \`[a-zA-Z0-9]\`;
// Matches any string that is a single alphanumeric character.
io:println("Pattern \`[a-zA-Z0-9]\` matches \`a\`: ", alphanumericPattern.isFullMatch("a"));
// Declare a RegExp using the \`fromString\` constructor method.
// \`string:RegExp\` is an alias for \`regexp:RegExp\`.
string:RegExp alphanumericPattern2 = re \`[a-zA-Z0-9]\`;
io:println("Pattern \`[a-zA-Z0-9]\` matches \`a1\`: ", alphanumericPattern2.isFullMatch("a1"));
// Construct a \`RegExp\` value from a string using the \`fromString\` langlib function.
string:RegExp pattern = check regexp:fromString("HELLO*");
// Matches any string that starts with "HELLO" and ends with zero or more characters.
io:println("Pattern \`HELLO*\` matches \`HELLO\`: ", pattern.isFullMatch("HELLO"));
Expand All @@ -22,13 +28,13 @@ public function main() returns error? {
boolean result = re \`a+\`.isFullMatch("aaa");
io:println("Pattern \`a+\` matches \`aaa\`: ", result);
// Interpolation can be used to change the pattern dynamically.
// Interpolations can be used to construct the pattern dynamically.
string literal = "xyz";
string:RegExp pattern2 = re \`abc|\${literal}\`;
// Matches any string that starts with "abc" or "XYZ".
io:println("Pattern \`abc|\${\\"xyz\\"}\` matches \`xyz\`: ", pattern2.isFullMatch("xyz"));
// RegExp supports writing Unicode general category patterns.
// The \`RegExp\` type supports Unicode general category patterns.
// Characters are matched based on their Unicode properties.
string:RegExp pattern3 = re \`\\p{Ll}\`;
io:println("Pattern \`\\\\p{Ll}\` matches \`a\`: ", "a".matches(pattern3));
Expand All @@ -38,12 +44,15 @@ public function main() returns error? {
string:RegExp pattern4 = re \`\\P{P}\`;
io:println("Pattern \`\\\\p{P}\` matches \`0\`: ", "0".matches(pattern4));
// RegExp supports matching characters according to the script using Unicode script patterns.
// The \`RegExp\` type supports matching characters according to the script using Unicode script patterns.
string:RegExp pattern5 = re \`\\p{sc=Latin}\`;
regexp:Span? latinValue = pattern5.find("aεЛ");
io:println("Pattern \`\\\\p{sc=Latin}\` matches \`aεЛ\`: ", (<regexp:Span>latinValue).substring());
regexp:Span? findResult = pattern5.find("aεЛ");
// The \`find\` function returns nil if no match is found. Since we know a
// match is found here, use the \`ensureType\` function to narrow the type down to \`regexp:Span\`.
regexp:Span latinValue = check findResult.ensureType();
io:println("Pattern \`\\\\p{sc=Latin}\` matches \`aεЛ\`: ", latinValue.substring());
// RegExp supports non-capturing groups to control the behavior of regular expression patterns.
// The \`RegExp\` type supports non-capturing groups to control the behavior of regular expression patterns.
// The \`i\` flag will ignore the case of the pattern.
string:RegExp pattern6 = re \`(?i:BalleRINA)\`;
io:println("Pattern \`(?i:BalleRINA)\` matches \`Ballerina\`: ", "Ballerina".matches(pattern6));
Expand Down Expand Up @@ -79,9 +88,9 @@ export function RegexpType({ codeSnippets }) {

<p>
A <code>RegExp</code> value can be created by using the regexp template
expression or calling the <code>fromString</code> method of the{" "}
expression or calling the{" "}
<a href="https://lib.ballerina.io/ballerina/lang.regexp/latest#fromString">
lang.regexp
<code>fromString</code> method of the lang.regexp
</a>{" "}
module.
</p>
Expand Down Expand Up @@ -224,6 +233,8 @@ export function RegexpType({ codeSnippets }) {
<pre ref={ref1}>
<code className="d-flex flex-column">
<span>{`\$ bal run regexp_type.bal`}</span>
<span>{`Pattern \`[a-zA-Z0-9]\` matches \`a\`: true`}</span>
<span>{`Pattern \`[a-zA-Z0-9]\` matches \`a1\`: false`}</span>
<span>{`Pattern \`HELLO*\` matches \`HELLO\`: true`}</span>
<span>{`Pattern \`a+\` matches \`aaa\`: true`}</span>
<span>{`Pattern \`abc|\${"xyz"}\` matches \`xyz\`: true`}</span>
Expand Down Expand Up @@ -263,7 +274,7 @@ export function RegexpType({ codeSnippets }) {
<span>&#8226;&nbsp;</span>
<span>
<a href="https://lib.ballerina.io/ballerina/lang.regexp">
RegExp API Docs
<code>regexp</code> langlib API Docs
</a>
</span>
</li>
Expand All @@ -273,7 +284,17 @@ export function RegexpType({ codeSnippets }) {
<span>&#8226;&nbsp;</span>
<span>
<a href="https://lib.ballerina.io/ballerina/lang.string">
string API Docs
<code>string</code> langlib API Docs
</a>
</span>
</li>
</ul>
<ul style={{ marginLeft: "0px" }} class="relatedLinks">
<li>
<span>&#8226;&nbsp;</span>
<span>
<a href="/learn/by-example/ensureType-function/">
The <code>ensureType</code> function
</a>
</span>
</li>
Expand Down

0 comments on commit 0a6f0a3

Please sign in to comment.