Page 1 of 2

APL chars into HTML docs

Posted: Tue Dec 31, 2013 1:10 am
by mario.sacco
The best (fastest and most effective) way I've found is to transform the code in JPEG (printscreen, crop, file, save as ...) and put it into html as image.

Any more intelligent advice is welcome!

Thanks,
Mario

Re: APL chars into HTML docs

Posted: Tue Dec 31, 2013 8:24 am
by Morten|Dyalog
Hi Mario!

I think the "correct" way (which should also be faster that using images) is to insert UTF-8 encoded text into an HTML page. You can create UTF-8 encoded text easily using the following incantation:

Code: Select all

⎕UCS 'UTF-8' ⎕UCS text

The rightmost (dyadic) ⎕UCS does the UTF-8 encoding, it returns numbers between 0 and 255, the leftmost (monadic) ⎕UCS turns that into text (you want to be using the Unicode edition of APL to avoid worrying about translations).

Once you have this, you need a little HTML to wrap it - I think the smallest amount of HTML you can get away with looks something like the following.

Code: Select all

<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>My APL Code</title>
   <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
</head>
<style type="text/css">
   pre {font-family: APL385 Unicode;}
</style>

<body>
   <pre>
   (APL CODE GOES HERE)
   </pre>
</body>
</html>

The interesting bits are the declaration of the content as being charset=UTF-8, and the style line which declares that all "pre" tags should use font APL385 Unicode. I don't know how much HTML you know, if that doesn't make sense please ask some more questions. If you can't rely on the font to be present on the client, you need a few more tricks, I'll have to dig up the recipe for embedding the font, let me know if you need this.

P.S. I experimented with this as follows: I put the HTML above into a file, replacing the text "(APL CODE GOES HERE)" with a single exclamation point. Then:

Code: Select all

'c:\temp\aplhtmltemplate.txt'⎕NTIE ¯1
z←⎕NREAD ¯1 80,⎕NSIZE ¯1
⎕NUNTIE ¯1
'c:\temp\aplhtmlexample.htm'⎕NCREATE ¯2
z[z⍳'!']←⊂⎕UCS 'UTF-8' ⎕UCS' {⍵[⍋⍵]}' ⍝ Replace ! with UTF-8 text
z←∊z ⍝ Enlist (⎕ML←1)
z ⎕NAPPEND ¯2
⎕NUNTIE ¯2

Re: APL chars into HTML docs

Posted: Tue Dec 31, 2013 12:20 pm
by Fiona|Dyalog
An alternative method exists if you're using cascading style sheets. This method makes for cleaner HTML as the definition only has to be entered once.

In the css, define a class that you can apply to APL characters. For example:

@font-face {
font-family: APLFont;
src: url(http://www.dyalog.com/apl-font-keyboard.htm);
}

@font-face {
font-family: APLFont;
src: local("APL385 Unicode"),
url(http://www.dyalog.com/apl-font-keyboard.htm);
}

.APLFont {
font-family: APLFont, APL385;
}


As long as your HTML page references the stylesheet, APL characters can be included within HTML span tags with the correct class assignment. For example:

<span class="APLFont">⎕ML</span>

Re: APL chars into HTML docs

Posted: Tue Dec 31, 2013 5:10 pm
by Roger|Dyalog
Please confirm that you have to have what looks like two definitions of the font-family, both named "APLFont". It would make more sense to me if the second were named

@font-face {
font-family: APL385;
src: local("APL385 Unicode"),
url(http://www.dyalog.com/apl-font-keyboard.htm);
}

But then I don't know CSS.

Re: APL chars into HTML docs

Posted: Tue Dec 31, 2013 5:44 pm
by Brian|Dyalog
Both Morten's and Fiona's replies get you closer, but not all the way...

There are reserved symbols in HTML called HTML entities.
The less-than and greater-than symbols are two examples of HTML entities.
So in APL,
3<4
would need to be
3&lt;4
in HTML.

Fortunately ⎕XML will do the conversion for us...

For this, I wrote APLToHTML for MiServer (Dyalog's APL-based web server). I've included a version below that I modified to be stand-alone.

Code: Select all

     ∇ HTML←APLToHTML APL;CR
[1]   ⍝ returns APL code formatted for HTML
[2]   ⍝ APL is a simple scalar, vector, or vector of simple vectors of APL code
[3]   ⍝ HTML is the HTML translation suitable for embedding in a web page
[4]   ⍝ assumes ⎕ML←1 (for ∊ = enlist)
[5]    CR←⎕UCS 13 ⍝ carriage return
[6]    :If 1<|≡APL ⋄ APL←1↓∊CR∘,¨APL ⋄ :EndIf ⍝ if nested vector of vectors
[7]    :Trap 0 ⋄ HTML←3↓¯4↓'whitespace' 'preserve'⎕XML 1 3⍴0 'x'APL ⍝ attempt to convert
[8]    :Else ⋄ HTML←APL ⍝ fallback to just using the APL
[9]    :EndTrap
[10]   HTML←('<pre style="font-family:APL385 Unicode">'),CR,HTML,CR,'</pre>'
     ∇


Example:
      APLToHTML '3<4' 'foo&1 ⍝ start foo in a separate thread'
<pre style="font-family:APL385 Unicode">
3&lt;4
foo&amp;1 ⍝ start foo in a separate thread
</pre>

Note, if you want to use Fiona's suggestion of making an APLFont class, you could change line 10 to read:
      HTML←('<pre class="APLFont">'),CR,HTML,CR,'</pre>'

Re: APL chars into HTML docs

Posted: Tue Dec 31, 2013 5:49 pm
by Brian|Dyalog
Roger,

The two font-family definitions in Fiona's post are because Internet Explorer deals with fonts differently than other browsers. IE uses Embedded OpenType (hence the .eot extension) whilst the rest of the world uses TrueType fonts (.ttf extension). Welcome to the world of cross-browser compatibility...

Re: APL chars into HTML docs

Posted: Wed Jan 01, 2014 3:25 pm
by Dick Bowman
Just to muddy the waters a little - or perhaps clear them - I was a little puzzled by Brian's remarks about < and > because I've been casually copy/pasting them into HTML pages for quite a while now without even thinking of them as special cases.

Just confirmed that they migrate transparently in my little regime (Firefox for the browser, BlueGriffon for HTML editing) - and a simple CSS entry as per Fiona to set the font (but not even bothering with the SPAN in the HTML). Maybe it's a result of always trying to use Unicode, maybe BlueGriffon is taking care of things for me.

Another example of a little more of my ignorance going a long way - haven't been much troubled by problems with the APL character set for many months (and just did a spot-check to see one of my pages correctly rendering itself in IE11 - can't recall using that before - with just the one font-family definition). But as Phil Last once kindly pointed out - I tend to keep on doing simple things until they break.

Re: APL chars into HTML docs

Posted: Wed Jan 01, 2014 5:52 pm
by Roger|Dyalog
Brian and Dick are both correct about < and >, I think. Usually (on several popular browsers), a < in HTML is fine as long as the immediately following character is not one of tags. So 3<4 is fine, but 3<a is not, because it can be confused with the <a href="..."> tag. To be safe you can use &lt; and &gt; instead of < and >; to be terse and convenient you can continue to use < and > and just fix up the (surprisingly) few cases where it matters.

An amusing problem arises if you want to talk about &lt; and &gt; in your HTML without having them converted into < and >. Whence &amp;lt; and &amp;gt;.

Re: APL chars into HTML docs

Posted: Thu Jan 02, 2014 3:24 pm
by mario.sacco
At the moment I tried both Morten & Fiona code and both perform fine.

I know about HTML and XML but I cannot consider myself as an "expert". So my impression is that Fiona code doesn't force the reader to have APL385 font installed on her/his client. Am I right?

Thanks also to Brian, Dick and Roger for the tip&tricks to not irritate the HTML using its reserved words (that I'll try).

About Morten Code in rows 4 you wrote ⎕CREATE instead of ⎕NCREATE, a minor mistake just to avoid problems to non-expert Dyalog APL readers.

Solved this a second question ... :-) what about the use of APL385 characters into a LaTex doc?

Re: APL chars into HTML docs

Posted: Fri Jan 03, 2014 9:01 am
by Morten|Dyalog
mario.sacco wrote:About Morten Code in rows 4 you wrote ⎕CREATE instead of ⎕NCREATE
Bizarre, I could have sworn I just copy/pasted that from my APL session(?). Thanks, I have corrected the post.
mario.sacco wrote:What about the use of APL385 characters into a LaTex doc?
I think I know someone who is doing this, will ask and get back to you.