Skip to content

Commit d70ce5b

Browse files
committed
Add README
1 parent 4bb0f42 commit d70ce5b

File tree

9 files changed

+978
-34
lines changed

9 files changed

+978
-34
lines changed

LICENSE.md

Lines changed: 636 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 198 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,201 @@
22

33
A library to auto convert URLs to links in text.
44

5-
## Installation via
5+
## Installation via Composer
6+
7+
Add this to composer.json require block.
8+
9+
``` json
10+
{
11+
"require": {
12+
"asika/autolink": "1.*"
13+
}
14+
}
15+
```
16+
17+
## Getting Started
18+
19+
This is a quick start to convert URL to link:
20+
21+
``` php
22+
use Asika\Autolink\Linker;
23+
24+
$text = Linker::convert($text);
25+
$text = Linker::convertEmail($text);
26+
```
27+
28+
## Use Autolink Object
29+
30+
Create the object:
31+
32+
``` php
33+
use Asika\Autolink\Autolink;
34+
35+
$autolink = new Autolink;
36+
```
37+
38+
Create with options.
39+
40+
``` php
41+
$options = array(
42+
'strip_scheme' => false,
43+
'text_limit' => false,
44+
'auto_title' => false
45+
);
46+
47+
$schemes = array('http', 'https', 'skype', 'itunes');
48+
49+
$autolink = new Autolink($options, $schemes);
50+
```
51+
52+
## Convert Text
53+
54+
This is an example text:
55+
56+
``` html
57+
This is Simple URL:
58+
http://www.google.com.tw
59+
60+
This is SSL URL:
61+
https://www.google.com.tw
62+
63+
This is URL with multi-level query:
64+
http://example.com/?foo[1]=a&foo[2]=b
65+
```
66+
67+
We convert all URLs.
68+
69+
``` php
70+
$text = $autolink->convert($text);
71+
```
72+
73+
Output:
74+
75+
``` php
76+
This is Simple URL:
77+
<a href="http://www.google.com.tw">http://www.google.com.tw</a>
78+
79+
This is SSL URL:
80+
<a href="https://www.google.com.tw">https://www.google.com.tw</a>
81+
82+
This is URL with multi-level query:
83+
<a href="http://example.com/?foo[1]=a&amp;foo[2]=b">http://example.com/?foo[1]=a&amp;foo[2]=b</a>
84+
```
85+
86+
### Add Attributes
87+
88+
``` php
89+
$text = $autolink->convert($text, array('class' => 'center'));
90+
```
91+
92+
All link will add this attributes:
93+
94+
``` php
95+
This is Simple URL:
96+
<a href="http://www.google.com.tw" class="center">http://www.google.com.tw</a>
97+
98+
This is SSL URL:
99+
<a href="https://www.google.com.tw" class="center">https://www.google.com.tw</a>
100+
```
101+
102+
## Options
103+
104+
### `text_limit`
105+
106+
We can set this option by constructor or setter:
107+
108+
``` php
109+
$auitolink->textLimit(50);
110+
111+
$text = $autolink->convert($text);
112+
```
113+
114+
The link text will be:
115+
116+
``` php
117+
http://campus.asukademy.com/learning/job/84-fin...
118+
```
119+
120+
Use Your own limit handler:
121+
122+
``` php
123+
$auitolink->textLimit(function($url)
124+
{
125+
return substr($url, 0, 50) . '...';
126+
});
127+
```
128+
129+
Or use Pretty shorter:
130+
131+
``` php
132+
$auitolink->textLimit(function($url)
133+
{
134+
return \Asika\Autolink\Linker::shorten($url, 15, 6);
135+
});
136+
```
137+
138+
Output:
139+
140+
``` text
141+
http://campus.asukademy.com/....../84-find-interns......
142+
```
143+
144+
### `auto_title`
145+
146+
Use AutoTitle to force add title on anchor element.
147+
148+
``` php
149+
$autolink->autoTitle(true);
150+
151+
$text = $autolink->convert($text);
152+
```
153+
154+
Output:
155+
156+
``` html
157+
<a href="http://www.google.com.tw" title="http://www.google.com.tw">http://www.google.com.tw</a>
158+
```
159+
160+
### `strip_scheme`
161+
162+
Strip Scheme on link text:
163+
164+
``` php
165+
$auitolink->stripScheme(true);
166+
167+
$text = $autolink->convert($text);
168+
```
169+
170+
Output
171+
172+
``` html
173+
<a href="http://www.google.com.tw" >www.google.com.tw</a>
174+
```
175+
176+
## Scheme
177+
178+
You can add new scheme to convert URL begin with it, foe example: `vnc://example.com`
179+
180+
``` php
181+
$autolink->addScheme('skype')
182+
->addScheme('vnc');
183+
```
184+
185+
Default schemes is `http, https, ftp, ftps`.
186+
187+
## Link Builder
188+
189+
If you don't want to use `<a>` element as your link, you can set a callback to build link HTML.
190+
191+
``` php
192+
$autolink->setLinkBuilder(function($url, $attribs)
193+
{
194+
$attribs['src'] = htmlspecialchars($url);
195+
196+
return (string) \Windwalker\Html\HtmlElement('img', null, $attribs);
197+
});
198+
```
199+
200+
See: [Windwalker Html Package](https://github.com/ventoviro/windwalker-html)
201+
202+

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
"name": "asika/autolink",
33
"description": "Auto convert url to link anchor.",
44
"type": "library",
5+
"homepage": "https://github.com/asika32764/php-autolink",
6+
"license": "GPL-3.0+",
57
"require": {
68
"windwalker/dom": "~2.0@stable"
79
},
@@ -10,7 +12,7 @@
1012
},
1113
"autoload": {
1214
"psr-4": {
13-
"Asika\\": "src/"
15+
"Asika\\Autolink\\": "src/"
1416
}
1517
},
1618
"license": "LGPL-3.0+",

src/Autolink.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
* @license GNU General Public License version 2 or later;
77
*/
88

9-
namespace Asika;
9+
namespace Asika\Autolink;
1010

1111
use Windwalker\Dom\HtmlElement;
1212

1313
/**
1414
* The Autolink class.
1515
*
16-
* @since {DEPLOY_VERSION}
16+
* @since 1.0
1717
*/
1818
class Autolink
1919
{
@@ -68,7 +68,7 @@ public function __construct($options = array(), $schemes = array())
6868
*
6969
* @return string
7070
*/
71-
public function render($text, $attribs = array())
71+
public function convert($text, $attribs = array())
7272
{
7373
$self = $this;
7474

@@ -82,7 +82,7 @@ function($matches) use ($self, $attribs)
8282

8383
if (!$inElements)
8484
{
85-
return $self->convert($matches[0], $attribs);
85+
return $self->link($matches[0], $attribs);
8686
}
8787

8888
return $matches[0];
@@ -99,7 +99,7 @@ function($matches) use ($self, $attribs)
9999
*
100100
* @return string
101101
*/
102-
public function renderEmail($text, $attribs = array())
102+
public function convertEmail($text, $attribs = array())
103103
{
104104
$regex = "/(([a-zA-Z]*=\")*\S+@\S+\.\S+)/";
105105

@@ -130,7 +130,7 @@ function($matches) use ($attribs)
130130
*
131131
* @return string
132132
*/
133-
public function convert($url, $attribs = array())
133+
public function link($url, $attribs = array())
134134
{
135135
$content = $url;
136136

src/LinkHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* @license GNU General Public License version 2 or later;
77
*/
88

9-
namespace Asika;
9+
namespace Asika\Autolink;
1010

1111
/**
1212
* The LinkHelper class.
1313
*
14-
* @since {DEPLOY_VERSION}
14+
* @since 1.0
1515
*/
1616
class LinkHelper
1717
{

src/Linker.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/**
3+
* Part of php-autolink project.
4+
*
5+
* @copyright Copyright (C) 2015 {ORGANIZATION}. All rights reserved.
6+
* @license GNU General Public License version 2 or later;
7+
*/
8+
9+
namespace Asika\Autolink;
10+
11+
/**
12+
* The Linker class.
13+
*
14+
* @method static string convert() convert($url, $sttribs = array())
15+
* @method static string convertEmail() convertEmail($url, $sttribs = array())
16+
* @method static string link() link($url, $sttribs = array())
17+
*
18+
* @since 1.0
19+
*/
20+
class Linker
21+
{
22+
/**
23+
* Property instance.
24+
*
25+
* @var Autolink
26+
*/
27+
protected static $instance;
28+
29+
/**
30+
* getInstance
31+
*
32+
* @param array $options
33+
* @param array $schemes
34+
*
35+
* @return Autolink
36+
*/
37+
public static function getInstance($options = array(), $schemes = array())
38+
{
39+
if (static::$instance instanceof Autolink)
40+
{
41+
return static::$instance;
42+
}
43+
44+
return static::$instance = new Autolink($options, $schemes);
45+
}
46+
47+
/**
48+
* Method to set property instance
49+
*
50+
* @param Autolink $instance
51+
*
52+
* @return void
53+
*/
54+
public static function setInstance($instance)
55+
{
56+
static::$instance = $instance;
57+
}
58+
59+
/**
60+
* __callStatic
61+
*
62+
* @param string $name
63+
* @param array $args
64+
*
65+
* @return mixed
66+
*/
67+
public static function __callStatic($name, $args)
68+
{
69+
$instance = static::getInstance();
70+
71+
if (is_callable(array($instance, $name)))
72+
{
73+
return call_user_func_array(array($instance, $name), $args);
74+
}
75+
76+
throw new \BadMethodCallException(sprintf('Method: %s::%s not exists.', 'Autolink', $name));
77+
}
78+
}

0 commit comments

Comments
 (0)