Pharo by Example (vol 1)

repository·pharo5·Indexed 42 days ago

https://github.com/squarebracketassociates/updatedpharobyexample

A practical guide to the Pharo programming language and live coding environment. This repository contains an older version of the book as a collection of Pillar-formatted documentation files, including guidelines for contributions, Pillar image and code block syntax, and support for the sba-book LaTeX class.

Tokens
3.7K
Snippets
13
Records
19
Agent score
53%

What's inside Pharo by Example

  1. Quickstart: Use Highlight.js on a web page

    pharo5

    To use Highlight.js on a web page, include the library, a CSS theme, and call hljs.initHighlightingOnLoad(). The library will automatically find and highlight code inside <pre><code> tags by attempting to auto-detect the language.

    To explicitly specify a language when auto-detection fails, use the class attribute on the <code> tag with the language name (e.g., class="html") or use the language- or lang- prefixes.

    To disable highlighting for a specific block, use the nohighlight class.

    <link rel="stylesheet" href="/path/to/styles/default.css">
    <script src="/path/to/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    
    <!-- Explicit language -->
    <pre><code class="html">...</code></pre>
    
    <!-- Disable highlighting -->
    <pre><code class="nohighlight">...</code></pre>
  2. Add missing languages to CDN-hosted Highlight.js

    pharo5

    The standard CDN-hosted package of Highlight.js does not include all languages to keep the file size small. If a required language is missing, you can manually include it by adding a script tag pointing to the specific language file from a CDN (e.g., cdnjs).

    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/languages/go.min.js"></script>
  3. Manually initialize highlighting with highlightBlock and configure

    pharo5

    For more control over what and when to highlight, use hljs.highlightBlock(block) and hljs.configure(options). This allows you to target specific elements instead of relying on the automatic initHighlightingOnLoad() behavior.

    If you are using a container that does not preserve line breaks, you can configure Highlight.js to use <br> tags by setting useBR: true in the configure method.

    // Example using jQuery to manually highlight blocks
    $(document).ready(function() {
      $('pre code').each(function(i, block) {
        hljs.highlightBlock(block);
      });
    });
    
    // Example with custom configuration for non-preserving containers
    hljs.configure({useBR: true});
    
    $('div.code').each(function(i, block) {
      hljs.highlightBlock(block);
    });
  4. How to contribute to Updated Pharo By Example

    pharo5

    Contributions follow the GitHub fork-and-pull workflow:

    1. Fork the repository.
    2. Create a topic branch using the naming convention: initials_ChapterName_my_topic_description (e.g., git checkout -b dz_PharoTour_update_screen_snapshots).
    3. Commit your changes to that branch.
    4. Submit a Pull Request.

    For detailed instructions on Pillar syntax and LaTeX setup, refer to the CONTRIBUTING.md file in the repository. For technical questions, contact the pharo-dev mailing list.

  5. Install Highlight.js via CDN or Module

    pharo5

    Highlight.js can be used via CDN, downloaded as a custom build, or installed as a module on a server.

    Important Notes:

    • Do not link directly to GitHub source code. The library requires a specific build to function correctly.
    • CDN limitations: CDN files do not include all languages to keep the file size small. If a language is missing, you can manually add it by including the specific language script (e.g., go.min.js) after the main library script.
    • Almond/Optimization: If using an optimizer like Almond, you must specify the module name (e.g., r.js -o name=hljs ...).
    <!-- Adding a missing language from CDN -->
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/languages/go.min.js"></script>
    # Example r.js command for optimization
    r.js -o name=hljs paths.hljs=/path/to/highlight out=highlight.js
  6. Use the SBAbook LaTeX class

    pharo5

    The sbabook class is built on the memoir document class and is designed for specific semantic markup and typographic styles.

    Important Requirements:

    • Use LuaLaTeX as your compiler instead of pdflatex to ensure fonts load correctly.
    • The class uses polyglossia for language settings, not babel.

    Basic preamble structure:

    \documentclass{sbabook}
    \setmainlanguage{english} % this is polyglossia, not babel
    \begin{document}
    
    ...and off you go!
    
    \end{document}
  7. Create Pharo_Icon_v0.9.svg from pharo-logos.zip

    pharo5

    To generate the Pharo_Icon_v0.9.svg asset, follow this workflow involving Inkscape and manual optimization:

    1. Setup:
      • Download pharo-logos.zip from http://www.pharo-project.org/pharo-download/media-files.
      • Import pharo-0.8.1.pdf into Inkscape.
    2. Inkscape Processing:
      • Ungroup all SVG elements.
      • Recalculate relative to absolute coordinates.
      • Split SVG elements if necessary (e.g., for the Pharo letter glare effect).
      • (Re-)Group associated parts.
      • Replace all bitmap parts with vector-only equivalents (specifically for the shadow).
      • Reorganize references between shapes and color-gradients/filters to eliminate duplicates.
      • Export from Inkscape without including Inkscape-specific XML elements or attributes.
    3. Optimization:
      • Use the SVG Optimiser to shorten long coordinate system numbers (e.g., convert 25.772936550317932 to 25.77).
      • Use a UTF-8 capable text editor for final touches.
      • Ensure SVG elements have descriptive ("speaking") names.
    4. Icon-Specific Adjustments:
      • Set background's fill-opacity to 0.
      • Set flare's opacity to 0.7 (increased from 0.5 for better contrast).
      • Note: water-splashes and flare of the icon-part will extend out of the stylized "o" (minimal intervention).
  8. Quick Start with Highlight.js

    pharo5

    To use Highlight.js on a web page with automatic language detection, link a style sheet, include the highlight.pack.js library, and call hljs.initHighlightingOnLoad(). This method automatically finds and highlights code within <pre><code> tags.

    If automatic detection fails, you can manually specify the language by adding a class to the <code> tag using the language- or lang- prefix (e.g., class="language-html").

    To prevent a specific block from being highlighted, use the nohighlight class.

    <link rel="stylesheet" href="/path/to/styles/default.css">
    <script src="/path/to/highlight.pack.js"></script>
    <script>hljs.initHighlightingOnLoad();</script>
    
    <!-- Manual language specification -->
    <pre><code class="html">...</code></pre>
    
    <!-- Disable highlighting -->
    <pre><code class="nohighlight">...</code></pre>
  9. General Documentation Style Guidelines

    pharo5

    When contributing documentation, follow these formatting rules:

    • Line Wrapping: Hard-wrap lines at 80 characters where possible (except for ordered/unordered list elements, which must remain on a single line for Pillar parsing).
    • Spacing: Use exactly one space between sentences.
    • Titles: Do not use periods in section titles.
    • Inline Code: Use Pillar monospace notation ==Object== for all inline mentions of objects, messages, variables, or packages.
  10. Run Highlight.js in a Web Worker

    pharo5

    To prevent large code blocks from freezing the browser UI, you can offload the syntax highlighting process to a Web Worker.

    Main Script Implementation: Listen for the load event, select the code element, create a new Worker, and set up an onmessage handler to update the element's innerHTML with the worker's result.

    Worker Script (worker.js) Implementation: Use importScripts to load highlight.pack.js, then use self.hljs.highlightAuto(event.data) to process the text and postMessage the result back to the main thread.

    // In your main script
    addEventListener('load', function() {
      var code = document.querySelector('#code');
      var worker = new Worker('worker.js');
      worker.onmessage = function(event) { code.innerHTML = event.data; }
      worker.postMessage(code.textContent);
    })
    
    // In worker.js
    onmessage = function(event) {
      importScripts('<path>/highlight.pack.js');
      var result = self.hljs.highlightAuto(event.data);
      postMessage(result.value);
    }