1
0
Fork 0
mirror of https://github.com/Oreolek/oreolek.ru.git synced 2024-05-21 02:18:20 +03:00

Smartypants for Markdown and new markup tag

This commit is contained in:
Alexander Yakovlev 2013-06-13 14:07:07 +07:00
parent 660f53989b
commit e34c840b80
7 changed files with 1686 additions and 10 deletions

View file

@ -0,0 +1,97 @@
<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH.'vendors/smartypants/smartypants.php');
class Markdown extends Kohana_Markdown {
/* Transformations that occur *within* block-level tags */
protected $span_gamut = array(
"parseSpan" => -30,
"do_local_images" => 5,
"do_images" => 10,
"do_anchors" => 20,
"doAutoLinks" => 30,
"do_smartypants" => 35,
"encode_amps_and_angles" => 40,
"do_italics_and_bold" => 50,
"do_hard_breaks" => 60,
);
/**
* Turn custom image shortcuts into <img> tags with preview.
* Shortcut is: !thumb[alt text](url "optional title")
* @see do_images
*
* @param string The markdown getting transformed.
* @return string String that will replace the tag.
*/
protected function do_local_images($text)
{
$text = preg_replace_callback('{
( # wrap whole match in $1
!thumb\[
('.$this->nested_brackets_re.') # alt text = $2
\]
\s? # One optional whitespace character
\( # literal paren
[ \n]*
(?:
<(\S*)> # src url = $3
|
('.$this->nested_url_parenthesis_re.') # src url = $4
)
[ \n]*
( # $5
([\'"]) # quote char = $6
(.*?) # title = $7
\6 # matching quote
[ \n]*
)? # title is optional
\)
)
}xs',
array(&$this, '_do_local_images_callback'), $text);
return $text;
}
protected function _do_local_images_callback($matches)
{
$whole_match = $matches[1];
$alt_text = $matches[2];
$url = $matches[3] == '' ? $matches[4] : $matches[3];
$title =& $matches[7];
$alt_text = $this->encode_attribute($alt_text);
$url = $this->encode_attribute($url);
$result = "<a href=\"$url\"><img src=\"".Model_Photo::generate_thumbnail_path($url)."\" alt=\"$alt_text\"";
/* $title already quoted */
if (isset($title)) {
$title = $this->encode_attribute($title);
$result .= " title=\"$title\"";
}
$result .= $this->suffix.'</a>';
return $this->hash_part($result);
}
/**
* Smartypants transformation
* @see APPPATH/vendors/smartypants/smartypants.php
**/
protected function do_smartypants($text)
{
return Smartypants($text);
}
protected function run_span_gamut($text)
{
foreach ($this->span_gamut as $method => $priority) {
$text = $this->$method($text);
}
return $text;
}
}

View file

@ -47,14 +47,8 @@ class Model_Photo extends ORM {
throw new HTTP_Exception_404('File not found');
return $image_path;
}
$parts = explode('.', $image_path);
$count = count($parts) - 2;
if ($count < 0) $count = 0;
$suffix = 'thumb';
if ($width) $suffix .= $width;
if ($height) $suffix .= '_' . $height;
$parts[$count] .= '_' . $suffix;
$thumbnail_path = implode('.', $parts);
$thumbnail_path = $this->generate_thumbnail_path($image_path, $width, $height);
if (!is_file(DOCROOT.$thumbnail_path)) {
$image = Image::factory(DOCROOT.$image_path);
@ -65,6 +59,20 @@ class Model_Photo extends ORM {
return $thumbnail_path;
}
public static function generate_thumbnail_path($image_path, $width = 0, $height = 0)
{
if ($width == 0) $width = Kohana::$config->load('common.thumbnail_width');
if ($height == 0) $height = Kohana::$config->load('common.thumbnail_height');
$parts = explode('.', $image_path);
$count = count($parts) - 2;
if ($count < 0) $count = 0;
$suffix = 'thumb';
if ($width) $suffix .= $width;
if ($height) $suffix .= '_' . $height;
$parts[$count] .= '_' . $suffix;
return implode('.', $parts);
}
protected function get_thumbnail_file_path($width = NULL, $height = NULL)
{
return DOCROOT.$this->get_thumbnail_path($width, $height);

View file

@ -13,7 +13,7 @@
{{#date}}
<div class="date">{{date}}</div>
{{/date}}
<div class="hyphenate"><a class="link_view" href="{{{view_link}}}">{{name}}</a></div>
<div class="hyphenate"><a class="link_view" href="{{{view_link}}}">{{{name}}}</a></div>
{{#edit_link}}
<div><a class="link_edit" href="{{{edit_link}}}">Редактировать</a></div>
<div><a class="link_delete" href="{{{delete_link}}}">Удалить</a></div>

View file

@ -0,0 +1,36 @@
PHP SmartyPants & Typographer
Copyright (c) 2005-2013 Michel Fortin
<http://michelf.ca/>
All rights reserved.
Original SmartyPants
Copyright (c) 2003-2004 John Gruber
<http://daringfireball.net/>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name "SmartyPants" nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.
This software is provided by the copyright holders and contributors "as
is" and any express or implied warranties, including, but not limited
to, the implied warranties of merchantability and fitness for a
particular purpose are disclaimed. In no event shall the copyright owner
or contributors be liable for any direct, indirect, incidental, special,
exemplary, or consequential damages (including, but not limited to,
procurement of substitute goods or services; loss of use, data, or
profits; or business interruption) however caused and on any theory of
liability, whether in contract, strict liability, or tort (including
negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.

View file

@ -0,0 +1,349 @@
PHP SmartyPants Typographer
===========================
Version 1.0.1 - Sun 23 Jan 2013
by Michel Fortin
<http://michelf.ca/>
Original SmartyPants by John Gruber
<http://daringfireball.net/>
Introduction
------------
This is a special version of PHP SmartyPants with extra features. See
<http://www.michelf.com/projects/php-smartypants/typographer/> for
details.
PHP SmartyPants is a free web publishing plug-in for WordPress and
Smarty template engine that easily translates plain ASCII punctuation
characters into "smart" typographic punctuation HTML entities.
SmartyPants can also be invoked as a standalone PHP function.
PHP SmartyPants is a port to PHP of the original SmartyPants written
in Perl by John Gruber.
SmartyPants can perform the following transformations:
* Straight quotes (`"` and `'`) into "curly" quote HTML entities
* Backtick-style quotes (` ``like this'' `) into "curly" quote HTML
entities
* Dashes (`--` and `---`) into en- and em-dash entities
* Three consecutive dots (`...`) into an ellipsis entity
This means you can write, edit, and save using plain old ASCII straight
quotes, plain dashes, and plain dots, but your published posts (and
final HTML output) will appear with smart quotes, em-dashes, and proper
ellipses.
SmartyPants does not modify characters within `<pre>`, `<code>`,
`<kbd>`, or `<script>` tag blocks. Typically, these tags are used to
display text where smart quotes and other "smart punctuation" would not
be appropriate, such as source code or example markup.
### Backslash Escapes ###
If you need to use literal straight quotes (or plain hyphens and
periods), SmartyPants accepts the following backslash escape sequences
to force non-smart punctuation. It does so by transforming the escape
sequence into a decimal-encoded HTML entity:
Escape Value Character
------ ----- ---------
\\ &#92; \
\" &#34; "
\' &#39; '
\. &#46; .
\- &#45; -
\` &#96; `
This is useful, for example, when you want to use straight quotes as
foot and inch marks:
6\'2\" tall
translates into:
6&#39;2&#34; tall
in SmartyPants's HTML output. Which, when rendered by a web browser,
looks like:
6'2" tall
Installation and Requirement
----------------------------
PHP SmartyPants require PHP version 4.0.5 or later.
### WordPress ###
WordPress already include a filter called "Texturize" with the same
goal as SmartyPants. You could still find some usefulness to
PHP SmartyPants if you are not happy enough with the standard algorithm.
PHP SmartyPants works with [WordPress][wp], version 1.2 or later.
[wp]: http://wordpress.org/
1. To use PHP SmartyPants with WordPress, place the "smartypants.php"
file in the "plugins" folder. This folder is hidden inside
"wp-content" at the root of your site:
(site home)/wp-content/plugins/smartypants.php
2. Activate the plugin with the administrative interface of WordPress.
In the "Plugins" section you will now find SmartyPants. To activate
the plugin, click on the "Activate" button on the same line than
SmartyPants. Your entries will now be filtered by PHP SmartyPants.
Note: It is not possible at this time to apply a different set of
filters to different entries. All your entries will be filtered by
PHP SmartyPants if the plugin is active. This is currently a limitation
of WordPress.
### In your programs ###
You can use PHP SmartyPants easily in your current PHP program. Simply
include the file and then call the `SmartyPants` function on the text
you want to convert:
include_once "smartypants.php";
$my_text = SmartyPants($my_text);
### With Smarty ###
If your program use the [Smarty][sm] template engine, PHP SmartyPants
can now be used as a modifier for your templates. Rename
"smartypants.php" to "modifier.smartypants.php" and put it in your
smarty plugins folder.
[sm]: http://smarty.php.net/
Options and Configuration
-------------------------
Settings are specified by editing the value of the `$smartypants_attr`
variable in the "smartypants.php" file. For users of the Smarty template
engine, the "smartypants" modifier also takes an optional attribute where
you can specify configuration options, like this:
`{$var|smartypants:1}` (where "1" is the configuration option).
Numeric values are the easiest way to configure SmartyPants's behavior:
"0"
Suppress all transformations. (Do nothing.)
"1"
Performs default SmartyPants transformations: quotes (including
backticks-style), em-dashes, and ellipses. `--` (dash dash) is
used to signify an em-dash; there is no support for en-dashes.
"2"
Same as smarty_pants="1", except that it uses the old-school
typewriter shorthand for dashes: `--` (dash dash) for en-dashes,
`---` (dash dash dash) for em-dashes.
"3"
Same as smarty_pants="2", but inverts the shorthand for dashes: `--`
(dash dash) for em-dashes, and `---` (dash dash dash) for en-dashes.
"-1"
Stupefy mode. Reverses the SmartyPants transformation process,
turning the HTML entities produced by SmartyPants into their ASCII
equivalents. E.g. `&#8220;` is turned into a simple double-quote
(`"`), `&#8212;` is turned into two dashes, etc. This is useful if you
wish to suppress smart punctuation in specific pages, such as
RSS feeds.
The following single-character attribute values can be combined to
toggle individual transformations from within the smarty_pants
attribute. For example, to educate normal quotes and em-dashes, but not
ellipses or backticks-style quotes:
$smartypants_attr = "qd";
Or inside a Smarty template:
{$var|smartypants:"qd"}
"q"
Educates normal quote characters: (`"`) and (`'`).
"b"
Educates ` ``backticks'' ` double quotes.
"B"
Educates backticks-style double quotes and ` `single' ` quotes.
"d"
Educates em-dashes.
"D"
Educates em-dashes and en-dashes, using old-school typewriter
shorthand: (dash dash) for en-dashes, (dash dash dash) for
em-dashes.
"i"
Educates em-dashes and en-dashes, using inverted old-school
typewriter shorthand: (dash dash) for em-dashes, (dash dash dash)
for en-dashes.
"e"
Educates ellipses.
"w"
Translates any instance of `&quot;` into a normal double-quote
character. This should be of no interest to most people, but of
particular interest to anyone who writes their posts using
Dreamweaver, as Dreamweaver inexplicably uses this entity to
represent a literal double-quote character. SmartyPants only
educates normal quotes, not entities (because ordinarily, entities
are used for the explicit purpose of representing the specific
character they represent). The "w" option must be used in
conjunction with one (or both) of the other quote options ("q" or
"b"). Thus, if you wish to apply all SmartyPants transformations
(quotes, en- and em-dashes, and ellipses) and also translate
`&quot;` entities into regular quotes so SmartyPants can educate
them, you should set the SMARTYPANTS_ATTR constant at the top of
the file to:
define( 'SMARTYPANTS_ATTR', "qDew" );
Inside a Smarty template, you could also pass the string as a
parameter:
{$var|smartypants:"qDew"}
### Algorithmic Shortcomings ###
One situation in which quotes will get curled the wrong way is when
apostrophes are used at the start of leading contractions. For example:
'Twas the night before Christmas.
In the case above, SmartyPants will turn the apostrophe into an opening
single-quote, when in fact it should be a closing one. I don't think
this problem can be solved in the general case -- every word processor
I've tried gets this wrong as well. In such cases, it's best to use the
proper HTML entity for closing single-quotes (`&#8217;` or `&rsquo;`) by
hand.
Bugs
----
To file bug reports or feature requests (other than topics listed in the
Caveats section above) please send email to:
<michel.fortin@michelf.com>
If the bug involves quotes being curled the wrong way, please send
example text to illustrate.
Version History
---------------
Typographer 1.0 (23 Jan 2013)
1.5.1f (23 Jan 2013):
* Fixed handling of HTML comments to match latest HTML specs instead of
doing it the old SGML way.
* Lowered WordPress filtering priority to avoid clashing with the
[caption] tag filter. Thanks to Mehdi Kabab for the fix.
Typographer 1.0 (28 Jun 2006)
* First public release of PHP SmartyPants Typographer.
1.5.1oo (19 May 2006, unreleased)
* Converted SmartyPants to a object-oriented design.
1.5.1e (9 Dec 2005)
* Corrected a bug that prevented special characters from being
escaped.
1.5.1d (6 Jun 2005)
* Correct a small bug in `_TokenizeHTML` where a Doctype declaration
was not seen as HTML, making curly quotes inside it.
1.5.1c (13 Dec 2004)
* Changed a regular expression in `_TokenizeHTML` that could lead
to a segmentation fault with PHP 4.3.8 on Linux.
1.5.1b (6 Sep 2004)
* Corrected a problem with quotes immediately following a dash
with no space between: `Text--"quoted text"--text.`
* PHP SmartyPants can now be used as a modifier by the Smarty
template engine. Rename the file to "modifier.smartypants.php"
and put it in your smarty plugins folder.
* Replaced a lot of spaces characters by tabs, saving about 4 KB.
1.5.1a (30 Jun 2004)
* PHP Markdown and PHP Smartypants now share the same `_TokenizeHTML`
function when loaded simultanously.
* Changed the internals of `_TokenizeHTML` to lower the PHP version
requirement to PHP 4.0.5.
1.5.1 (6 Jun 2004)
* Initial release of PHP SmartyPants, based on version 1.5.1 of the
original SmartyPants written in Perl.
Copyright and License
---------------------
Copyright (c) 2005-2013 Michel Fortin
<http://michelf.ca/>
All rights reserved.
Copyright (c) 2003-2004 John Gruber
<http://daringfireball.net/>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name "SmartyPants" nor the names of its contributors may
be used to endorse or promote products derived from this software
without specific prior written permission.

File diff suppressed because it is too large Load diff

@ -1 +1 @@
Subproject commit bca01a2a25c4c175730d1fdeb42b8ba1a7801893
Subproject commit 677c04dea9eca0dd71beaf5277f802b7bead8be0