Найти адреса сайта и преобразовать в гиперссылки.

Статус
Закрыто для дальнейших ответов.

LeonidB

Их бин
10 лет на форуме
Сообщения
2 470
Реакции
1 530
А уже не стал на этом заострять внимание. Но для чистоты эксперимента надо указать, что в ссылках бывает и 2 двоеточия:
Код:
http://opil.ouplaw.com/view/10.1093/law:epil/9780199231690/law-9780199231690-e873

С учётом двоеточий в ссылках:

Код:
#target Indesign
// --- Assumptions --- //
//https://forums.adobe.com/message/3187356 (original)
//http://forum.rudtp.ru/threads/najti-adresa-sajta-i-preobrazovat-v-giperssylki.63656 (edited by azz, _MBK_, Eugenyus, LeonidB)
// One document open
// Hyperlinks not necessarily labeled by character style
// You want all hyperlinks to be live
// Typesetter sometimes inserts a space or return in middle of URL to force a line break
// Space or return comes before period or slash per style manual
var doc=app.activeDocument;
try{
var cshyp = doc.characterStyles.add({name:"hyper"});
alert("Создан символьный стиль hyper");
}
catch (e){
var cshyp = doc.characterStyles.item("hyper");
}
// Compress stray www .'s
fn_grepChange (doc, "www(\\s|\\n)\\.", "www.", "hyper");
// Normalize to http://
fn_grepChange (doc, "(?<!\\/\\/)(www\\.([\\w\\-\\?\\=\\&]+([\\.\\/]?[\\%\\+\\#\\:]?)+)+)([\\l\\u\\d\\?\\%\\/])+", "http://$0", "hyper");
// Remove ( space or return ) before ( period or slash )
fn_grepChange (doc, "(https?\\:\\/\\/([\\w\\-\\?\\=\\&]+[\\.\\/]?)+)(\\s|\\n)(\\.|\\/)", "$1$4", "hyper");
fn_grepChange (doc, "(https?\\:\\/\\/.+)([\\s\\n]+)([\\.\\/])", "$1$3", "hyper");
fn_grepChange (doc, "(https?\\:\\/\\/.+)([\\s\\n]+)([\\.\\/])", "$1$3", "hyper");
// Символьный стиль hyper на все интернет-адреса
fn_grepChange (doc, "(https?\\:\\/\\/([\\w\\-\\?\\=\\&]+([\\.\\/\\%\\+\\#\\:])*)+)([\\l\\u\\d\\?\\%\\/])+", "$0", "hyper");

// ---- Main ---- //
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
app.findChangeGrepOptions.includeFootnotes = true;
// Regardless of character styles, find hyperlinks
app.findGrepPreferences.findWhat = '(https?\\:\\/\\/([\\w\\-\\?\\=\\&]+([\\.\\/\\%\\+\\#\\:])*)+)([\\l\\u\\d\\?\\%\\/])+';
var found = app.findGrep();
var report = "found and activated "+String(found.length)+" links\n";
// make hyperlinks
for(i=0; i<found.length; i++)
try    {
        var report = report+found[i].contents+"\n";
       
        var source = doc.hyperlinkTextSources.add(found[i]);  
        var dest=doc.hyperlinkURLDestinations.add(found[i].contents);
           
        doc.hyperlinks.add(source,dest, {visible:false, name:found[i].contents+"("+(i+1)+")"});
   
    } catch ( err) {};
// If exporting to PDF set include hyperlinks to true
PDFExportPreset.includeHyperlinks = true;
// To export to html, choose File-->Export for Dreamweaver
app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences = NothingEnum.nothing;
alert ( report );
function fn_grepChange (myDocument, regexFind, regexChange, cStyle) {
    // purpose: execute a grep change, otherwise known as a regex s///;
    //Initialize.
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;
    //Set the find options.
    app.findChangeGrepOptions.includeFootnotes = true;
    app.findChangeGrepOptions.includeHiddenLayers = false;
    app.findChangeGrepOptions.includeLockedLayersForFind = false;
    app.findChangeGrepOptions.includeLockedStoriesForFind = false;
    app.findChangeGrepOptions.includeMasterPages = false;
    //Search and change
    app.findGrepPreferences.findWhat = regexFind;
    app.changeGrepPreferences.changeTo = regexChange;
    app.changeGrepPreferences.appliedCharacterStyle= cStyle;
    myDocument.changeGrep();
 
    //Clear the find/change preferences.
    app.findGrepPreferences = NothingEnum.nothing;
    app.changeGrepPreferences = NothingEnum.nothing;
    }

// --- end of script --- //
 

Dorm

Топикстартер
10 лет на форуме
Сообщения
574
Реакции
1
А еще может быть ссылка без начала www или http ;) . Вот:
Код:
osce-academy.net

Ловил уже руками в тексте
 

LeonidB

Их бин
10 лет на форуме
Сообщения
2 470
Реакции
1 530
А еще может быть ссылка без начала www или http ;) . Вот:
Код:
osce-academy.net
Ловил уже руками в тексте
Такие точно только ручками. Ибо в таком виде могут быть не только гиперссылки. В тексте скрипта ловятся только те выражения без http:// , которые начинаются на www, и к ним уже добавляется http:// Иначе можно много дров наломать.
 

LeonidB

Их бин
10 лет на форуме
Сообщения
2 470
Реакции
1 530
Ну почему же?!! Можно совсем упороться и искать по домену (net com и т.д.). (шутка)
Искать-то можно, но добавлять к ним в тексте http:// (как в случае с адресами www) не стоит. А скрипт в нынешнем виде это делает (потому как в гиперссылках это должно быть).
 
Статус
Закрыто для дальнейших ответов.