Git Product home page Git Product logo

diagrams-builder's Introduction

Build Status

diagrams-builder provides backend-agnostic tools for dynamically turning code into rendered diagrams, using the hint wrapper to the GHC API. It supports conditional recompilation using hashing of diagrams source code, to avoid recompiling code that has not changed. It is useful for creating tools which compile diagrams code embedded in other documents. For example, it is used by the BlogLiterately-diagrams package (a plugin for BlogLiterately) to compile diagrams embedded in Markdown-formatted blog posts, and by the diagrams-latex.sty package for embedding diagrams in LaTeX documents (see below).

Executables specific to the cairo, svg, postscript, rasterific, and PGF backends are included. Each takes an input file and an expression to render and outputs an image file using the appropriate backend. You must explicitly enable whichever executables you want using flags like -fcairo, -fsvg, and so on.

A LaTeX package, diagrams-latex.sty, is also provided in the latex/ directory of the source distribution, which renders diagrams code found within diagram environments. It can make use of any of the diagrams-builder-cairo, diagrams-builder-postscript, or diagrams-builder-pgf executables, so if you want to use diagrams-latex.sty you should install diagrams-builder with the appropriate option. Note that diagrams-latex.sty is licensed under the GPL. For more information on using diagrams-latex.sty, see this tutorial.

diagrams-builder's People

Contributors

acowley avatar apirogov avatar bergey avatar byorgey avatar cchalmers avatar cdupont avatar fryguybob avatar iot-icore avatar jbransen avatar jeffreyrosenbluth avatar ozkutuk avatar sid-kap avatar yiding avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

diagrams-builder's Issues

LaTeX package for embedded diagram code

(Imported from http://code.google.com/p/diagrams/issues/detail?id=44. Original issue from [email protected] on September 2, 2011, 08:00:47 PM UTC)

It would be super-cool to provide a LaTeX package which allows you to embed diagrams code directly in a LaTeX document and have it rendered as part of the final output, without needing a preprocessor. In order to do this we need a command-line tool for turning diagrams code into images; see Issue 43.

I believe Ryan knows how to do this...

Ambiguous instance for <> coming from Data.Monoid and Diagrams.Prelude

stack: lts-11.9

perhaps this was already expected. So, (<>) are defined in both Data.Monoid and Diagrams.Prelude.
My workaround is actually just to remove the "Data.Monoid" in setDiagramsImports.
I do not know if this is a good solution or not.
I create this issue to raise awareness and hopefully the solution would be integrated into the main branch.
So that I can remove my local repo of diagrams-builder. Thank you very much for your consideration.

Cryptic error messages when using the diagrams-builder-cairo

Hi, when I compile my latex file using the cairo backend I get

Opening diagrams-latex stream diagrams/pretty-print-problem-diagrams-latex-fig1
.hs Error while compiling diagrams/pretty-print-problem-diagrams-latex-fig1.hs
/tmp/Diagram1804289383846930886.hs:4:8:
    Could not find module ‘Diagrams.Prelude’
    Use -v to see a list of the files searched for.
/tmp/Diagram1804289383846930886.hs:5:8:
    Could not find module ‘Diagrams.Backend.Cairo’
    Use -v to see a list of the files searched for.
/tmp/Diagram1804289383846930886.hs:6:8:
    Could not find module ‘Diagrams.TwoD.Text’
    Use -v to see a list of the files searched for.

Which was not the real bug. I had to catch the temporary file using a hacky loop in /tmp:

cd /tmp
while 1; do cat Diagram* >> bug.hs; done

remove the duplicates (the time it takes to remove the temporary file), and then compile it using:

ghc --make bug.hs

to get the real error message.
Happened also to this guy: https://groups.google.com/forum/#!topic/diagrams-discuss/DY3M38X0dbA.

Thanks.

versions used
diagrams-builder: 0.7.2.0
ghc: 7.10.2
diagrams-core: 1.3.0.3
diagrams-cairo: 1.3.0.5
NixOS: NixOS/nixpkgs@819a375

Missing pragma for GHC 7.10

Compiling a diagrams with GHC 7.10 gives:

Error while interpreting
example = square 1
/tmp/Diagram1804289383846930886.hs:10:1:
    Illegal equational constraint V t ~ V2
    (Use GADTs or TypeFamilies to permit this)
    When checking that ‘example’ has the inferred type
      example :: forall t. (TrailLike t, V t ~ V2) => t

The pragma TypeFamilies should be added.

Include ABI hash of dependencies in diagram hashes

Currently, diagrams are not rebuilt when packages they depend on change. A simple solution is to get ghc-pkg to dump out the ABI hash of dependent packages and include them in the hash we compute for each diagram. Some notes:

  • Have to figure out how to map from module names to packages.
  • Be careful to use the right package database, e.g. check if some hsenv (or cabal sandbox?) is active.

Add support for cabal-sandboxes during diagram compilation.

This issue concerns the case where the prerequisites for a diagram are installed in a cabal sandbox. diagrams-builder uses hint to interpret diagrams, which needs to be told how to find libraries that aren't installed in the global package database.

Support currently exists for HSENV style sandboxes, where the $HSENV environment variable is set (to something, anything), and the $PACKAGE_DB_FOR_GHC is set to "-package-db=/path/to/.cabal-sandbox/*-packages.conf.d".

While it works, this functionality should probably be extended to work seamlessly with cabal sandboxes, and should almost certainly be documented.

Automatically generated temporary modules have invalid names under `base-4.10`

See this StackOverflow question.

diagrams-builder calls openTempFile (see here) to get a temporary file in which to output a Haskell module. It then uses the base name of the resulting file as the name of the module (as is required).
Although the documentation for openTempFile says the created file will have a name of the form "fooXXX.ext where XXX is some random number", the behavior of openTempFile changed recently so that it now always includes a hyphen (I think this was released in base-4.10). The problem is that a hyphen cannot be part of a Haskell module name, so now diagrams-builder fails with parse errors when it tries to compile the auto-generated modules.

More accurate hashed rebuilding

When checking whether to rebuild a diagram based on a hash, there are several things currently not taken into account, which is super annoying. They should be taken into account so that changing them produces a different hash.

There are a couple ways this could be done: one is just to stick any extra information in a string constant at the end of the code being hashed, e.g.

dia = square 1
foo = bar

__EXTRA_HASH_INFO = "width=200;height=200"

Alternatively (probably a better idea) is to provide an extra argument of an arbitrary string which will get concatenated together with the source code right before passing it through the hash.

In particular, the things which should affect the hash but don't are:

  1. The diagram size (and arbitrary diagram options in general). This is easy to add, as outlined above.

  2. The contents of any local imported module. This is important because a common design pattern is to make a .hs file with a bunch of common definitions and then import it into each embedded diagram in a document. e.g. if you're writing a document about Foos you might make Foos.hs with some general definitions for creating diagrams about Foos. The problem is that just changing Foos.hs does not cause anything to get recompiled so you end up having to manually delete the contents of the cache.

    A quick outline of how to accomplish this: parse the diagrams code (using HSE), find all the imports, and find out which ones are local (i.e. they correspond to files relative to the current directory). For each such local import, hash ITS source and include the resulting hash string as extra info, just like the width and height are included as above.

Qualified imports not in released version?

I was trying to run the build script for diagrams-doc, and I noticed that the qimports function (which was added on March 21, 2015) that the script needs is in the master and qualified branches here, but not in the release branch or on Hackage/Stackage.

Is there a plan to move this function into the release version of this package? (This isn't a huge problem for me, since I should be able to make stack use the master branch version of diagrams-builder. However, I thought it might be worth bringing up here.)

Bad defaults in diagrams-latex.sty

  • When using diagrams-latex.sty, it appears that if the outputdir argument is not supplied, it will default to /, making it effectively required
  • Using backend=pgf seems to require extension=pgf and input options both be specified

prettyPrint doesn't respect pattern matching

Hi, I have the following code in one of my diagrams in latex:

import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':'-':d:chars) = [superScript '-',superScript d] ++ format chars
format ('^':d:chars) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)

which doesn't render the diagram at all.
I checked the diagrams-builder-cairo's temporary haskell file, and found that it didn't translate the format function's pattern matching as expected:

{-# LANGUAGE NoMonomorphismRestriction, TypeFamilies,
  FlexibleContexts #-}
module Diagram1804289383846930886 where
import Diagrams.Prelude
import Diagrams.Backend.Cairo
import Diagrams.TwoD.Text
superScript c
  = case c of
        '0' -> '\8304'
        '1' -> '\185'
        '2' -> '\178'
        '3' -> '\179'
        '4' -> '\8308'
        '5' -> '\8309'
        '6' -> '\8310'
        '7' -> '\8311'
        '8' -> '\8312'
        '9' -> '\8313'
        '+' -> '\8314'
        '-' -> '\8315'
        '=' -> '\8316'
        '(' -> '\8317'
        ')' -> '\8318'
        'i' -> '\8305'
        'n' -> '\8319'
        otherwise -> c
format ((('^' : '-') : d) : chars)
  = [superScript '-', superScript d] ++ format chars
format (('^' : d) : chars) = [superScript d] ++ format chars
format (c : chars) = [c] ++ format chars
format [] = []
dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)

which in turn causes the following error message (it actually produces something cryptic, see #23):

bug.hs:27:17:
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’
    In the pattern: '-'
    In the pattern: '^' : '-'
    In the pattern: ('^' : '-') : d

bug.hs:28:35:
    Couldn't match expected type ‘Char’ with actual type ‘[[Char]]’
    In the first argument of ‘superScript’, namely ‘d’
    In the expression: superScript d

bug.hs:29:10:
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’
    In the pattern: '^'
    In the pattern: '^' : d
    In the pattern: ('^' : d) : chars

bug.hs:29:43:
    Couldn't match expected type ‘Char’ with actual type ‘[[Char]]’
    In the first argument of ‘superScript’, namely ‘d’
    In the expression: superScript d

bug.hs:30:23:
    Couldn't match expected type ‘Char’ with actual type ‘[[Char]]’
    In the expression: c
    In the first argument of ‘(++)’, namely ‘[c]’

bug.hs:32:20:
    Couldn't match type ‘Char’ with ‘[[Char]]’
    Expected type: [[[Char]]]
      Actual type: [Char]
    In the first argument of ‘format’, namely ‘"A^-3d^2+3A^-1d+3A"’
    In the first argument of ‘text’, namely
      ‘(format "A^-3d^2+3A^-1d+3A")’
    In the first argument of ‘(#)’, namely
      ‘text (format "A^-3d^2+3A^-1d+3A")’

If I replace it by explicitly writing down the parenthesis precedence like so:

\begin{diagram}[width=400,height=75]
import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':('-':(d:(chars)))) = [superScript '-',superScript d] ++ format chars
format ('^':(d:chars)) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)

it works fine.

Here is a latex document with both examples:

% Created 2015-11-08 Sun 03:21
\documentclass[11pt]{article}
\tolerance=1000
\usepackage{amsmath}
\usepackage[backend=cairo, extension=pdf, outputdir=diagrams]{/home/amitai/github/haskell/diagrams/diagrams-builder/latex/diagrams-latex}
\usepackage{graphicx}
\author{Amitai Hoze}
\date{\today}
\title{Bug reproduction}
\begin{document}

\maketitle


\section{Reproducing the bug}
\label{sec:orgheadline1}
\begin{diagram}[width=400,height=75]
import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':'-':d:chars) = [superScript '-',superScript d] ++ format chars
format ('^':d:chars) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)
\end{diagram}

\section{Temporary fix}
\label{sec:orgheadline2}
\begin{diagram}[width=400,height=75]
import Diagrams.TwoD.Text

superScript c = case c of
            '0' -> '\x2070'
            '1' -> '\xb9'
            '2' -> '\xb2'
            '3' -> '\xb3'
            '4' -> '\x2074'
            '5' -> '\x2075'
            '6' -> '\x2076'
            '7' -> '\x2077'
            '8' -> '\x2078'
            '9' -> '\x2079'
            '+' -> '\x207A'
            '-' -> '\x207B'
            '=' -> '\x207C'
            '(' -> '\x207D'
            ')' -> '\x207E'
            'i' -> '\x2071'
            'n' -> '\x207F'
            otherwise -> c

format ('^':('-':(d:(chars)))) = [superScript '-',superScript d] ++ format chars
format ('^':(d:chars)) = [superScript d] ++ format chars
format (c:chars) = [c] ++ format chars
format [] = []

dia = text (format "A^-3d^2+3A^-1d+3A") # scale 0.2 <> strutX (7)
\end{diagram}
\end{document}

versions used
diagrams-builder: 0.7.2.0
ghc: 7.10.2
diagrams-core: 1.3.0.3
diagrams-cairo: 1.3.0.5
NixOS: NixOS/nixpkgs@819a375

Faster/optimized builds

When interpreting diagrams, diagrams-builder doesn't use any sort of optimization. I have just run into a case where this makes a really big difference, i.e. compiling a blog post takes a few seconds with -O2 and upwards of a minute without.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.