all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* HTML img tags
@ 2016-12-10 16:53 Hikaru Ichijyo
  2016-12-10 17:55 ` Eli Zaretskii
                   ` (4 more replies)
  0 siblings, 5 replies; 9+ messages in thread
From: Hikaru Ichijyo @ 2016-12-10 16:53 UTC (permalink / raw)
  To: help-gnu-emacs

Is there a mode, or some bit of Lisp code I can put in ~/.emacs, that
will let Emacs guess the width= and height= attributes of an image file
from the image itself when I create an img tag in HTML?  I realize
there's probably no way for Emacs to do that without calling external
programs as a subprocess, so it's unlikely, but it would certainly be
convenient, and there are some HTML authoring programs that will do that
for you.  The old KDE 3 app Quanta Plus did, but I'd rather do
everything in Emacs.

-- 
He that would make his own liberty secure must guard even his enemy from
oppression; for if he violates this duty, he establishes a precedent
that will reach to himself.
					--Thomas Paine


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-10 16:53 HTML img tags Hikaru Ichijyo
@ 2016-12-10 17:55 ` Eli Zaretskii
  2016-12-12 15:46 ` HASM
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: Eli Zaretskii @ 2016-12-10 17:55 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Hikaru Ichijyo <ichijyo@macross.sdf.jp>
> Date: Sat, 10 Dec 2016 10:53:02 -0600
> 
> Is there a mode, or some bit of Lisp code I can put in ~/.emacs, that
> will let Emacs guess the width= and height= attributes of an image file
> from the image itself when I create an img tag in HTML?

We have image-size, is that what you want?



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-10 16:53 HTML img tags Hikaru Ichijyo
  2016-12-10 17:55 ` Eli Zaretskii
@ 2016-12-12 15:46 ` HASM
  2016-12-14  4:44 ` James K. Lowden
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 9+ messages in thread
From: HASM @ 2016-12-12 15:46 UTC (permalink / raw)
  To: help-gnu-emacs


> Is there a mode, or some bit of Lisp code I can put in ~/.emacs, that
> will let Emacs guess the width= and height= attributes of an image file
> from the image itself when I create an img tag in HTML?

One needs to parse the headers of a lot of different file formats, so
not trivial to do all images types.  For the most common ones, on unix
you can call "file <file>" and parse the output.

-- HASM




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-10 16:53 HTML img tags Hikaru Ichijyo
  2016-12-10 17:55 ` Eli Zaretskii
  2016-12-12 15:46 ` HASM
@ 2016-12-14  4:44 ` James K. Lowden
  2016-12-14  9:05   ` B.V. Raghav
  2016-12-14  9:39 ` Lars Magne Ingebrigtsen
  2016-12-14  9:51 ` Konstantin Shakhnov
  4 siblings, 1 reply; 9+ messages in thread
From: James K. Lowden @ 2016-12-14  4:44 UTC (permalink / raw)
  To: help-gnu-emacs

On Sat, 10 Dec 2016 10:53:02 -0600
Hikaru Ichijyo <ichijyo@macross.sdf.jp> wrote:

> I realize there's probably no way for Emacs to do that without
> calling external programs as a subprocess

Not no way, but no reason to.  :-)  

> I'd rather do everything in Emacs.

Just to tease you a little bit, you don't really mean that.  You let
emacs rely on the OS to provide a machine abstraction, for instance.  I
doubt you want to write directly to the hardware in elisp.  

Once you accept there's no "everything", you're on a slippery slope.
The question becomes merely, "how can emacs most readily obtain the
information I want?"  And to that, I have an answer for you!  

The ImageMagick package includes a utility "identify" that surely
produces the information you need.  If I wanted something to "guess the
width= and height= attributes" of a file, I'd probably write a little
awk script to spit out the HTML I wanted, and invoke that
from emacs.  If I wanted to do everything in emacs, I guess I'd invoke
"identify" directly, and winnow its output in elisp.  

HTH. 

--jkl


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-14  4:44 ` James K. Lowden
@ 2016-12-14  9:05   ` B.V. Raghav
  0 siblings, 0 replies; 9+ messages in thread
From: B.V. Raghav @ 2016-12-14  9:05 UTC (permalink / raw)
  To: James K. Lowden; +Cc: help-gnu-emacs

"James K. Lowden" <jklowden@speakeasy.net> writes:

> The ImageMagick package includes a utility "identify" that surely
> produces the information you need.  If I wanted something to "guess the
> width= and height= attributes" of a file, I'd probably write a little
> awk script to spit out the HTML I wanted, and invoke that
>
Apt mention of the utility. It is quick enough as well. I prefer bash to
awk, but that hardly counts.

Here is a throw-away bash function that does the hop

img_size_attribs () {
  DIMS=`identify $1 | cut -d\  -f3`
  WIDTH=`echo $IDENT | cut -dx -f1`
  HEIGHT=`echo $IDENT | cut -dx -f2`
  echo -n "width=\"$WIDTH\" height=\"$HEIGHT\""
}

-- 
(B.V. Raghav)



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-10 16:53 HTML img tags Hikaru Ichijyo
                   ` (2 preceding siblings ...)
  2016-12-14  4:44 ` James K. Lowden
@ 2016-12-14  9:39 ` Lars Magne Ingebrigtsen
  2016-12-14  9:51 ` Konstantin Shakhnov
  4 siblings, 0 replies; 9+ messages in thread
From: Lars Magne Ingebrigtsen @ 2016-12-14  9:39 UTC (permalink / raw)
  To: help-gnu-emacs

Hikaru Ichijyo <ichijyo@macross.sdf.jp> writes:

> Is there a mode, or some bit of Lisp code I can put in ~/.emacs, that
> will let Emacs guess the width= and height= attributes of an image file
> from the image itself when I create an img tag in HTML?  I realize
> there's probably no way for Emacs to do that without calling external
> programs as a subprocess

I don't know of any such mode, but there's no need to call external
programs.  Just do a `create-image' on the image and then call
`image-size' on the result.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-10 16:53 HTML img tags Hikaru Ichijyo
                   ` (3 preceding siblings ...)
  2016-12-14  9:39 ` Lars Magne Ingebrigtsen
@ 2016-12-14  9:51 ` Konstantin Shakhnov
  2016-12-14 10:48   ` tomas
  4 siblings, 1 reply; 9+ messages in thread
From: Konstantin Shakhnov @ 2016-12-14  9:51 UTC (permalink / raw)
  To: help-gnu-emacs

On Sat, Dec 10, 2016 at 10:53:02AM -0600, Hikaru Ichijyo wrote:
> Is there a mode, or some bit of Lisp code I can put in ~/.emacs, that
> will let Emacs guess the width= and height= attributes of an image file
> from the image itself when I create an img tag in HTML?  I realize
> there's probably no way for Emacs to do that without calling external
> programs as a subprocess, so it's unlikely, but it would certainly be
> convenient, and there are some HTML authoring programs that will do that
> for you.  The old KDE 3 app Quanta Plus did, but I'd rather do
> everything in Emacs.

If you have imagemagic this will work:

(defun my-insert-html-image-tag (filename)
  "Insert html tag for image file"
  (interactive "fFile name: ")
  (insert
   (format "<img src=\"%s\" %s />"
	   filename
	   (replace-regexp-in-string
	    "\n" ""
	    (shell-command-to-string
	    (format "identify %s | sed -r 's/^.* ([0-9]+)x([0-9]+) .*/width=\\1 heght=\\2/'" filename))))))



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-14  9:51 ` Konstantin Shakhnov
@ 2016-12-14 10:48   ` tomas
  2016-12-14 13:27     ` Konstantin Shakhnov
  0 siblings, 1 reply; 9+ messages in thread
From: tomas @ 2016-12-14 10:48 UTC (permalink / raw)
  To: help-gnu-emacs

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Dec 14, 2016 at 12:51:02PM +0300, Konstantin Shakhnov wrote:
> On Sat, Dec 10, 2016 at 10:53:02AM -0600, Hikaru Ichijyo wrote:
> > Is there a mode, or some bit of Lisp code I can put in ~/.emacs, that
> > will let Emacs guess the width= and height= attributes of an image file
> > from the image itself when I create an img tag in HTML?  I realize
> > there's probably no way for Emacs to do that without calling external
> > programs as a subprocess, so it's unlikely, but it would certainly be
> > convenient, and there are some HTML authoring programs that will do that
> > for you.  The old KDE 3 app Quanta Plus did, but I'd rather do
> > everything in Emacs.
> 
> If you have imagemagic this will work:
> 
> (defun my-insert-html-image-tag (filename)
>   "Insert html tag for image file"
>   (interactive "fFile name: ")
>   (insert
>    (format "<img src=\"%s\" %s />"
> 	   filename
> 	   (replace-regexp-in-string
> 	    "\n" ""
> 	    (shell-command-to-string
> 	    (format "identify %s | sed -r 's/^.* ([0-9]+)x([0-9]+) .*/width=\\1 heght=\\2/'" filename))))))

Or, just "pulling in" the sed (and consolidating it with the already
present `replace-regexp-in-string'):

    (replace-regexp-in-string
      "^.* \\([0-9]+\\)x\\([0-9]+\\) \\(:?.\\|\n\\)*$"
      "width=\\1 height=\\2"
      (shell-command-to-string
        (format "identify %s" filename)))

The funny construction at the end of regexp \\(:.\\|\n\\) is there
because dot alone won't match the \n. I don't know whether this
can be expressed more readably/elegantly.

regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAlhRI2EACgkQBcgs9XrR2kagRACePjnCf2HvMgKRyTgOZJf4Kp7w
5oQAmgPsP4kypbZup6u8M+P3ff50GS0h
=U2c+
-----END PGP SIGNATURE-----



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: HTML img tags
  2016-12-14 10:48   ` tomas
@ 2016-12-14 13:27     ` Konstantin Shakhnov
  0 siblings, 0 replies; 9+ messages in thread
From: Konstantin Shakhnov @ 2016-12-14 13:27 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, Dec 14, 2016 at 11:48:01AM +0100, tomas@tuxteam.de wrote:
> Or, just "pulling in" the sed (and consolidating it with the already
> present `replace-regexp-in-string'):
>
>     (replace-regexp-in-string
>       "^.* \\([0-9]+\\)x\\([0-9]+\\) \\(:?.\\|\n\\)*$"
>       "width=\\1 height=\\2"
>       (shell-command-to-string
>         (format "identify %s" filename)))

Great idea.

> The funny construction at the end of regexp \\(:.\\|\n\\) is there
> because dot alone won't match the \n. I don't know whether this
> can be expressed more readably/elegantly.

"^.* \\([0-9]+\\)x\\([0-9]+\\) .*\n$" works well. Or
"^.*\\([0-9]+\\)x\\([0-9]+\\) .*\n?$" if we not sure if there is an '\n'
at the end of string



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2016-12-14 13:27 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-12-10 16:53 HTML img tags Hikaru Ichijyo
2016-12-10 17:55 ` Eli Zaretskii
2016-12-12 15:46 ` HASM
2016-12-14  4:44 ` James K. Lowden
2016-12-14  9:05   ` B.V. Raghav
2016-12-14  9:39 ` Lars Magne Ingebrigtsen
2016-12-14  9:51 ` Konstantin Shakhnov
2016-12-14 10:48   ` tomas
2016-12-14 13:27     ` Konstantin Shakhnov

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.