How to show math equations with DDoc? - d

I'd like to use math equations in documentation using DDoc. What's a good way to make it look good?
Something like:
/**
* Returns x + sqrt(2).
*/
double add_sqrttwo(double x) {
return x + sqrt(2);
}

You can import MathJax, and then put the math in-between \( \) or \[ \].
An example:
/**
* Macros:
* DDOC =
* <!DOCTYPE html>
* <html lang="en-US">
* <head>
* <script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"></script>
* <title>$(TITLE)</title>
* </head>
* <body><h1>$(TITLE)</h1>$(BODY)</body>
* </html>
*
*/
module example;
import std.math;
/**
* Jay! \( a + \sqrt{2} \)
*
* You must prefix parameter names with an underscore: \( _x + \sqrt{2} \).
*
*/
double add_sqrttwo(double x) {
return x + sqrt(2.0);
}

I'm afraid DDOC itself cannot do this.
You will have to use e.g. Javascript to accomplish this or pre-generate them as images.

If you are targeting HTML, there's a few options, though none are ddoc per se: you can link to an image with the formula (<img src="...">), you can put tags around it and process later (whether a post processor in your generation step or a Javascript library on the web), or you can try to write MathML in the document https://developer.mozilla.org/en-US/docs/Web/MathML/Authoring ...
You might also try using various unicode characters like returns X + √2 but that's also pretty limited.
But all of these are working around ddoc, not with it. ddoc just can't do it on its own.

Related

How do i assign a template on PageLayoutView Preview in TYPO3 10?

I would like to assign a template on my PageLayoutView preview in order to avoid writing HTML on my PHP files and better maintain it. How can i achieve that using TYPO3 10?
I would like to use it for example on the textmedia content element and add the subheader in.
In order to achieve that, you will have to use the Standalone view. I would write a function which i can call it whenever i want to use it. Normally i would include the function on a Helper Class but for the sake of this answer i will put it on the same class. So lets say we have the textmedia content element and we want to add fields on the backend Preview.
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Contains a preview rendering for the page module of CType="textmedia"
*/
class TextMediaPreviewRenderer implements PageLayoutViewDrawItemHookInterface
{
/**
* Preprocesses the preview rendering of a content element of type "textmedia"
*
* #param \TYPO3\CMS\Backend\View\PageLayoutView $parentObject Calling parent object
* #param bool $drawItem Whether to draw the item using the default functionality
* #param string $headerContent Header content
* #param string $subheaderContent Subheader content
* #param string $itemContent Item content
* #param array $row Record row of tt_content
*/
public function preProcess(
PageLayoutView &$parentObject,
&$drawItem,
&$headerContent,
&$subheaderContent,
&$itemContent,
array &$row
) {
if ($row['CType'] === 'textmedia') {
$standaloneView = $this->getStandAloneConfig();
/*Disable TYPO3's default backend view configuration */
$drawItem = false;
/*Assign all the results to the backend */
$standaloneView->assignMultiple([
'title' => $parentObject->CType_labels[$row['CType']],
'type' => $row['CType'],
'content' => $row,
]);
$itemContent .= $standaloneView->render();
}
}
public function getStandAloneConfig()
{
$standaloneView = GeneralUtility::makeInstance(StandaloneView::class);
$standaloneView->setLayoutRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Layouts/']);
$standaloneView->setTemplateRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Templates/']);
$standaloneView->setPartialRootPaths([10,'EXT:your_extension/Resources/Private/Backend/Partials/']);
$standaloneView->setFormat('html');
$standaloneView->setTemplate('PageLayoutView.html');
return $standaloneView;
}
What is happening is the following
First, we get the StandAlone configuration. In this configuration (getStandAloneConfig()) we set the path to where our templates, partials and layouts are. Then we assign the type (html) and then the name of the template where our preview is going to be built on (PageLayoutView.html).
After that we reset everything that TYPO3 writes on the Preview ($drawItem = false;) so we can write our own.
Last and not least we assign the variables which we are going to use on the HTML file.
Under your_extension/Resources/Private/Backend/Templates/PageLayoutView.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
<f:switch expression="{type}">
<f:case value="textmedia">
<f:render partial="Textmedia" arguments="{_all}"/>
</f:case>
<f:case value="text">
<f:render partial="Text" arguments="{_all}"/>
</f:case>
<f:defaultCase>
<f:render partial="Header" arguments="{_all}"/>
</f:defaultCase>
</f:switch>
</html>
I personally use the PageLayoutView.html as a controller which decides, which partial should be rendered. So i assign the variable {type} and i render the partial based on its value.
Under your_extension/Resources/Private/Backend/Partials/Textmedia.html
<html xmlns:f="http://typo3.org/ns/TYPO3/CMS/Fluid/ViewHelpers" data-namespace-typo3-fluid="true">
<p><strong class="element_title">{title}</strong></p>
<table class="element_table">
<tbody>
<tr>
<th>Subheader</th>
<td>{content.subheader}</td>
</tr>
</tbody>
</table>
</html>
Now you can look what is inside the {content} variable and render it on your HTML.
If you want to style your Preview you can do the following: Under your ext_tables.php add the following line:
$GLOBALS['TBE_STYLES']['stylesheet'] = 'EXT:your_extension/Resources/Public/Css/Backend/page-layout-view.css';
There's a pretty easy way. Just set an alternative path for the preview templates.
\TYPO3\CMS\Backend\View\PageLayoutView::tt_content_drawItem():
// If the previous hook did not render something,
// then check if a Fluid-based preview template was defined for this CType
// and render it via Fluid. Possible option:
// mod.web_layout.tt_content.preview.media = EXT:site_mysite/Resources/Private/Templates/Preview/Media.html
if ($drawItem) {
$fluidPreview = $this->renderContentElementPreviewFromFluidTemplate($row);
if ($fluidPreview !== null) {
$out .= $fluidPreview;
$drawItem = false;
}
}

Google Custom Search with XSLT

I'm trying to implement the google custom search engine to a website running with xslt.
This code is located in the head section
(function() {
var cx = '..............';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//www.google.com/cse/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
And this snippet in the body
<gcse:search></gcse:search>
But i only get following error Warning: DOMDocument::load(): Namespace prefix google on search is not defined
Some idea why? Do i need a special xmlns?
Cheers
I am trying to make sense of https://developers.google.com/custom-search/docs/element#overview and your posted question. I am kind of guessing but based on https://developers.google.com/custom-search/docs/element#html5 try to replace <gcse:search></gcse:search> in your code with <div class="gcse-search"></div>, that way I hope your XSLT input is namespace well-formed and your attempt to include the Google search in the transformation result works.
Just bringing out how to add attributes example from the links shared by Martin above:
HTML5-valid div tags
You can use HTML5-valid div tags as long as you observe these guidelines:
The class attribute must be set to gcse-XXX
Any attributes must be prefixed with data-.
For example:
<div class="gcse-searchbox" data-resultsUrl="http://www.example.com"
data-newWindow="true" data-queryParameterName="search" >

Joomla 2.5 content plugin

I'm trying to deploy a simple plugin to a Joomla 2.5 installation. The code in the plugin that is outside the class declaration runs and adds the two script tags to the head. However, the code within does nothing. I can't change the $article->title or $article->text. I've copy and pasted, verbatim from different articles, but everything seems to talk only about 1.5. The 1.7 stuff that I do find only mentions changing onPrepareContent to onContentPrepare. Neither seems to do anything. I would appreciate any help!
<?php
// No direct access.
defined( '_JEXEC' ) or die( 'Restricted access' );
class plgContentPicasaGallery extends JPlugin
{
/**
*
* #param string The context of the content being passed to the plugin.
* #param mixed An object with a "text" property.
* #param array Additional parameters.
* #param int Optional page number. Unused. Defaults to zero.
* #return boolean True on success.
*/
public function onContentBeforeDisplay($context, &$article, &$params, $page = 0)
{
if (is_object($article)) {
$article->text = "omfg, wtf?";
return true;
} else {
$article = "omfg, I'm not an object, wtf?";
return true;
}
}
}
Joomla documentation & tutorials a little bit out dated, new framework changed few things.
To find proper signatures simply look at /plugins/content/... files.
Below is proper function signature & phpdoc for onContentPrepare.
/**
* #param string The context of the content being passed to the plugin.
* #param object The article object. Note $article->text is also available
* #param object The article params
* #param int The 'page' number
*/
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
...
}
My noobishness with Joomla prevailed over my good sense. I was editing the plugin files on the server and I was expecting that to update the plugin. Thanks for the help!
you can use this method
jimport('joomla.form.helper');
$urla= JRequest::getVar('id');
$urlview= JRequest::getVar('view');
if ($urlview=='article')
{}
if ($urla==10<- number id article )
{}
i know framework joomla is good but its for understanding method

Doxygen groups and modules index

I am creating a Doxygen document for my project. Recently, I have grouped related classes using \addtogroup tag. After this, I have got a module tab in my documentation. It shows all modules. I want to add some description right below module name below the module name on the same page. How can I do it using Doxygen ?
Here's my tag
/*! \addtogroup test test
* Test Testing a group in doxygen
* #{
*/
You have to write a dedicated .h file which contains only comments.
For each group you define a comment like this:
/** #defgroup FooGroup
*
* This module does yada yada yada
*
*/
Then you assign definition to the group (even on different files) like this:
/** #addtogroup FooGroup */
/*#{*/
/** Summon a goat
*
* #param name The name of the goat;
* #return The summoned goat;
*/
Goat summon_goat (const char *name);
...
...
/*#}*/
EDIT:
Also this is how it becomes.
See the "Detailed Description"? You can also add code snippet and examples within the #verbatim and #endverbatim commands.

How do I linkify text using ActionScript 3

I have a text that I want to linkify (identify URLs and convert them to HTML links). The text could be multi-line, and could contain multiple urls like the example below.
My current actionscript code looks like this
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
private function init():void {
var str:String = "#stack the website for google is http://www.google.com and gmail is http://gmail.com";
//Alert.show(linkify(str),"Error");
txtStatus.htmlText = linkify(str);
}
private function linkify(texty:String):String {
//return texty.replace("/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g",function(m):String { return m.linkify(m);});
//return texty.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(m):String {return m.linkify(m);}).replace(/(^|[^\w])(#[\d\w\-]+)/g, function(m2):String{return '#' + m2.substr(1) + ''; });
var pattern:RegExp = /[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g;
var match:String = pattern.exec(texty);
return texty.replace(pattern,'<a href="' + match + '">' +
match + '</a>');
}
]]>
</mx:Script>
The problem with the above script is that it recognizes the first match and uses that across. Also how do i do it for #?
Any help is highly appreciated.
ooph ... why does everybody use regex these days, to accomplish super simple tasks? also, you forgot, that "+" is a valid character for URLs, as a replacement for space, and even an awful lot of other characters may be used, so your pattern would not even match accordingly ...
well, anyway, have a look at AS3 regex metacharacters ...
that'll GREATLY improve your expression's readability and is much more robust...
i'd go with something like this, really:
var r:RegExp = /(?:http|https):\/\/\S*/g;
trace(str.replace(r, function (s:String,...rest):String {
return '' + s + ''
} ));
but the actual point, was the global flag ...
good luck then ... :)
greetz
back2dos