Template talk:example
Contents[hide] |
[edit] Run this code
Since liveworkspace hasn't been accessible for a while, can this template be redone to execute code at Coliru? That site allows file I/O, has clang and gcc and boost etc. For example, my cpp/locale/codecvt example (uses file) http://coliru.stacked-crooked.com/view?id=0b5a9d7f1b90ee53d75f89736c30dde5-f674c1a6d04c632b71a62362c0ccfc51 or someone else's example from Stackoverflow earlier today (uses boost to write to file and then shows it): http://coliru.stacked-crooked.com/view?id=f183a5f4d18ba357db2dda4ce38b1c06-da882c8a8f99862423d4c4ab7f16877a --Cubbi 08:23, 18 August 2013 (PDT)
- I agree that is worth to look for alternatives to liveworkspace. I played a bit with coliru and think it could be a good replacement. However, I didn't notice anything mentioning API usage or even ways to contact the webmaster. Any chance you know who's behind the website? P12 05:45, 19 August 2013 (PDT)
- It's http://stackoverflow.com/users/75889/stackedcrooked - I'll try to catch him in chat. --Cubbi 06:14, 19 August 2013 (PDT)
- I also agree that this would be awesome, and it would be worth reaching out to the person(s) behind Coliru to see if they are interested. For liveworkspace, we ended up using a pretty simple API that just basically just used a compressed and base64-encoded program+options (like which compiler to use) and used the resulting string directly in a URL. This approach was limited by the informal ~2k URL length limit, but that hopefully isn't much of an issue for our examples. (Alternatively we could POST the payload.) --Nate 07:07, 19 August 2013 (PDT)
- So apparently there is an API --Cubbi 19:25, 19 August 2013 (PDT)
You can compile a program on Coliru with a simple HTTP POST request:
- Simple Bash program:
~ $ curl http://coliru.stacked-crooked.com/compile -d '{"cmd": "echo Hello World", "src": ""}' + echo Hello World Hello World
- Simple C++ program:
~ $ curl http://coliru.stacked-crooked.com/compile -d '{"cmd": "g++ main.cpp && ./a.out", "src": "#include <iostream>\nint main(){ std::cout << \"Hello World!\" << std::endl;}"}'
You can use JavaScript's JSON.stringify function to escape the C++ program text. --StackedCrooked 19:48, 19 August 2013 (PDT)
- That's great. I'll see if I can get something working with that API. --Nate 04:30, 20 August 2013 (PDT)
- I think we could do awesome things with this API. Imagine the "run the code" button bringing up an inline editor that allows running the code and seeing the results, all while still seeing the documentation above. P12 08:24, 20 August 2013 (PDT)
- Coliru currently explicitly enforces same-origin policy, so there's hardly a way users could use coliru without us proxying the requests in some way. P12 04:13, 21 August 2013 (PDT)
[edit] When no output is specified, an empty paragraph is generated instead.
If the output
parameter is not specified, an empty paragraph is generated after the example code — right where the output part of the example would be shown. I think its superfluous.
Here are some examples:
puts("Some code, with the output provided.");
Output:
Some code, with the output provided.
This post-example text is placed right after the output.
puts("Some code, but no output.");
This post-example text is separated from the example code by a blank line/paragraph where the output would be shown.
— Radix (talk) 16:46, 28 November 2020 (PST)
- After some experimenting on a replica of the original {{example}} wiki-template it became clear that the aforementioned glitch (extra paragraph) is on account of the two extra new-lines (to be deleted!) inside that wiki-template. The "patched" template is here: {{example_only}}.
[edit] Test results using the updated wiki-template
int main(){ __builtin_printf("Some code, but no output.\n");}
This post-example text is placed right after the output.
#include <iostream> int main() { std::cout << "Test of the \"example_only\" wiki-template.\n"; }
A post-example sample text...
Explanatory text
int main(){ __builtin_printf("Some code, but no output.\n");}
Possible output:
test output
This post-example text to be placed right after the output.
This section is incomplete Reason: no example |
Post-example text...
- --Space Mission (talk) 10:26, 25 February 2021 (PST)
[edit] To Admins: a request to update protected { {example} } template
Dear ADMINS!
Could you (slightly) update the protected template {{example}} to remove that really annoying superfluous paragraph (one big extra line), that is generated after every Example when no Output is specified (i.e. when the {{|output=}} clause is absent).
For comparison, no such superfluous paragraph is generated after the {{source}} template.
What should be done:
the code of the protected wiki-template {{example}} to be replaced with the following code (the only difference is that two new lines are removed):
{{#if:{{{code|}}} |<div class="t-example {{#if:{{{std|}}}|t-example-std-{{{std}}}}}"><!-- -->{{#if:{{{1|}}}| <p>{{trim|{{{1}}}}}</p> }}<div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div> {{source | 1={{{code|}}} | lang={{{lang|{{get lang}}}}}}} {{#if:{{{output|}}} | {{#ifeq: {{{p|}}} | true | Possible output: | Output:}} {{source | lang=text | 1={{{output}}}}} }} </div> | {{todo|reason=no example}}}}<noinclude> {{documentation|Template:source/doc}} </noinclude>
This "new" code works fine for the tested template {{example_only}}, as you can see (and test further) on this talk page (see above).
After this update (if applied:), the result can be observed on any page where there are more than one (mini-)example, e.g. on cpp/language/lambda (see the example that stays before the phrase "Within the body of a lambda"... ).
Thanks in advance.)
--Space Mission (talk) 13:16, 31 March 2021 (PDT)
- ✔ Done. --Space Mission (talk) 09:18, 6 April 2021 (PDT)
[edit] Protected edit request: Arguments to insert leading spaces
At Template talk:source#Protected edit request: Parameter for leading spaces, I've just requested the addition of a new parameter leading=
, to insert the given number of leading spaces before the displayed code. This is necessary because MediaWiki eats leading and trailing spaces on all arguments specified with a parameter name, such as 1=
or code=
.
Assuming that request is applied, I'd like to then request that Template:example be updated to accept parameters which it would pass to the relevant {{source|...}}
calls.
-
code-leading=
(for the code block) -
output-leading=
(for the example output)
To do that, after Template:source is updated (assuming it is), please change the template code here to:
{{#if:{{{code|}}} |<div class="t-example {{#if:{{{std|}}}|t-example-std-{{{std}}}}}"><!-- -->{{#if:{{{1|}}}| <p>{{trim|{{{1}}}}}</p> }}<div class="t-example-live-link"><div class="coliru-btn coliru-btn-run-init">Run this code</div></div> {{source | 1={{{code|}}} | leading={{{code-leading|0}}} | lang={{{lang|{{get lang}}}}}}} {{#if:{{{output|}}} | {{#ifeq: {{{p|}}} | true | Possible output: | Output:}} {{source | lang=text | leading={{{output-leading|0}}} | 1={{{output}}}}} }} </div> | {{todo|reason=no example}}}}<noinclude> {{documentation|Template:source/doc}} </noinclude>
Thanks. -- FeRDNYC (talk) 18:30, 3 July 2021 (PDT)
[edit] Protected edit request: Argument to add compiler flags
Could you update the protected template {{example}} to allow passing custom flags to the compiler? GCC (and clang?) do not support #pragma STDC FENV_ACCESS ON, and so the example at cpp/numeric/math/rint is very confusingly broken (with rounding set downward, rint(-2.3) -> -2.f instead of -3.f --- surprisingly, given the preceding, lrint(-2.3) -> -3 correctly!). This can be fixed by passing -frounding-math to at least some of the GCC versions when I tested it on Coliru separately. Looking at the wiki template, I don't see a way to wire up even page-hardcoded flags (let alone enabling a UI for the user to experiment).
Thanks!
Will 172.69.134.153 06:26, 30 May 2023 (PDT)
- Seconded. The examples at cpp/language/transactional_memory require -fgnu-tm (see the footnote on that page) when compiling with gnu, but without args being available on the example template they generate errors instead of the examples' intended results. Clang also fails, but I'm not sure what flags it needs, yet. 2600:8800:1180:25:58FF:3443:D4A:3497 10:31, 7 October 2023 (PDT)